I'm not sure if I get your point, but from the first sentece I think you may need "weak events". That is, events that doesn't keep an strong reference to the listener so the garbarge collection can release the listeners without explicitly removing them from the event. You are using VB right? I don't really know, but, I think the "handles" syntax may be prone to that problem.
Ok, it goes like this, the listener is in a set of references that is kept for the event, so when you fire the event, all the objects in the set get called, as long as the object with events is alive all the objects that are in the set are kept alive, that is because the set is of strong refereneces (references that prevent garbage collection). So... even if your listeners are no longer in use and no longer reachable unless from the object with events, they are kept alive. It gets worst because it's common that listeners hold a reference of the object with events. So... a single listener alive can keep alive the whole thing. There are three solutions: - Add and remove listeners explicitly when needed (that includes finalizers and dispose), instead of using the "handles" syntax. And try to remember do not forgive to call dispose. - Avoid the events altogether. - Use weak events, those are events that allows garbage collection... but those aren't native of .NET Anyhow I think this is a good reading for this case: http://www.codeproject.com/KB/cs/WeakEvents.aspx (I hope it isn't too much C# for you). http://www.codeproject.com/KB/cs/WeakDelegateSet.aspx (you may find the code provided here useful, compile it and then reverse it to VB.NET with reflector or similar, or just use the dll if you can). Those articles dosn't have the best possible implementation, but the easier to use and undestand I've found so far... yep, it's an uncommon topic, sorry, but it's C#. Theraot On Sep 22, 5:10 pm, BB <[email protected]> wrote: > Hi all, > I have a big problem with objects that contain events. > > I found out that my application does not free memory when expected if > I use my class as declared: > > Public Class CMyClass > Inherits Object > Private fPropertyOne As String > Private fPropertyTwo As Integer > > Public Event OnSomeEvent(ByRef vSender As Object) > > End Class > > Testing with GC, I've got that creating this objects memory grows and > do not change when exit from the scope, but if I remove the event > declaration, memory get free as I suppose it has to do for the garbage > work. > The problem is very heavy using many objects like that. > I've tried with GC.Collect() but it does not change. > Is there something I should know? :( > > I'm using vb.net 2005 with .net 2.0 > > Thank you! > > EM
