Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Static Events Used By Static Classes

Reply
 
Old 03-04-2008   #1 (permalink)
Mike


 
 

Static Events Used By Static Classes

I am using a static event from the .NET framework and MSDN says:

"Because this is a static event, you must detach your event handlers
when your application is disposed, or memory leaks will result."

But I am using the event in a static class in a DLL so I can not
implement the disposable interface. Where should I detach the event?

Thanks,
Mike

My System SpecsSystem Spec
Old 03-04-2008   #2 (permalink)
Kevin Spencer


 
 

Re: Static Events Used By Static Classes

Hard to know without knowing what sort of application you're working with.
If it's a Windows Forms application, you can write an event handler for the
System.Windows.Forms.Application.ThreadExit or
System.Windows.Forms.Application.ApplicationExit events.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"Mike" <MLM450@xxxxxx> wrote in message
news:b05e9485-9320-4f03-912c-ba1ee4b3d840@xxxxxx
Quote:

>I am using a static event from the .NET framework and MSDN says:
>
> "Because this is a static event, you must detach your event handlers
> when your application is disposed, or memory leaks will result."
>
> But I am using the event in a static class in a DLL so I can not
> implement the disposable interface. Where should I detach the event?
>
> Thanks,
> Mike

My System SpecsSystem Spec
Old 03-04-2008   #3 (permalink)
Marc Gravell


 
 

Re: Static Events Used By Static Classes

Where is that quotation from? If talking about individual objects, then it
makes sense - since otherwise the static event subscription will prevent the
object from being collected (causing memory to grow, or at least not be
reclaimed). However, long-lived subscriptions will happily die when the
process (exe, etc) dies; at which point all the memory is reclaimed.

If you can post a link to the offending MSDN page, I might be able to give
more info...

Marc


My System SpecsSystem Spec
Old 03-04-2008   #4 (permalink)
Mike


 
 

Re: Static Events Used By Static Classes

This is used in a DLL. That DLL might be used by a Windows Forms app
but that is not gauranteed.
My System SpecsSystem Spec
Old 03-04-2008   #5 (permalink)
Mike


 
 

Re: Static Events Used By Static Classes

It is from the SystemEvents.TimeChanged event.

http://msdn2.microsoft.com/en-us/lib...mechanged.aspx
My System SpecsSystem Spec
Old 03-04-2008   #6 (permalink)
Marc Gravell


 
 

Re: Static Events Used By Static Classes

I've had a quick dig, and the unsubscribe is only doing standard delegate
operations - nothing fancy (unmanaged handles etc). As such, you don't need
to worry about anything here if you are just subscribing a static method to
a static event - it will get cleaned up happily when the process dies.

Static events only normally become an issue when you are listening to them
from instances, i.e.

public class Foo {
public Foo() {
SomeStaticClass.SomeEvent += this.SomeInstanceMethod; // risky
}
// ...
}

because with the above (if not correctly released) the static event can keep
many, many objects alive accidentally. You don't have this problem if you
have a static subscription:

public static class Bar {
static Bar() {
SomeStaticClass.SomeEvent += Bar.SomeStaticMethod; // fine
}
// ...
}

Marc


My System SpecsSystem Spec
Old 03-05-2008   #7 (permalink)
Kevin Spencer


 
 

Re: Static Events Used By Static Classes

Hi Mike,

In that case, you can implement a Dispose method and a Finalizer. The
Dispose method can do the clean-up, and if it is not used by the developer,
the Finalizer can call it conditionally. The Finalizer is always called when
the DLL is unloaded from memory. See the following MSDN article on how to
implement this pattern:

http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"Mike" <MLM450@xxxxxx> wrote in message
news:3fe6b8fd-5b65-4bb0-8fdc-fab7b27976d5@xxxxxx
Quote:

> This is used in a DLL. That DLL might be used by a Windows Forms app
> but that is not gauranteed.

My System SpecsSystem Spec
Old 03-05-2008   #8 (permalink)
Ben Voigt [C++ MVP]


 
 

Re: Static Events Used By Static Classes


"Marc Gravell" <marc.gravell@xxxxxx> wrote in message
news:efqGWxffIHA.4196@xxxxxx
Quote:

> Where is that quotation from? If talking about individual objects, then it
> makes sense - since otherwise the static event subscription will prevent
> the object from being collected (causing memory to grow, or at least not
> be reclaimed). However, long-lived subscriptions will happily die when the
> process (exe, etc) dies; at which point all the memory is reclaimed.
What about AppDomain unload, where the hosting process might be very
long-lived indeed?
Quote:

>
> If you can post a link to the offending MSDN page, I might be able to give
> more info...
>
> Marc
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
static ip Vista networking & sharing
Access a static member on a nested static class. PowerShell
Access a static member on a nested static class. PowerShell
static ip? Vista networking & sharing
Suggestion: New-Object's error message for static classes should explicitly describe correct use PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46