![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I class has a property "public IList<SomeClass> MyCollection". In this class' Dispose(bool) method I am setting this.MyCollection null. Do I need to remove all the objects from the list before doing this? |
|
An object will only be 'removed' by the GC if it is no longer referenced by anything. If I have wired up one of that object's events to an event handler do I need to remove it first (e.g. myObject.MyEvent -= new EventHandler(this.eventhandler) ? |
|
I create a new object and wire up its Success event to an event handler method. I then start this object running on a new thread (using Thread.Start()). When this event is handled and the object has finished executing, will it automatically go out of scope? Or, in the event handler to I have to first unwire the event (again using the -= syntax from Q2)? |
#3
| |||
| |||
|
|
I class has a property "public IList<SomeClass> MyCollection". In this class' Dispose(bool) method I am setting this.MyCollection null. Do I need to remove all the objects from the list before doing this? no An object will only be 'removed' by the GC if it is no longer referenced by anything. If I have wired up one of that object's events to an event handler do I need to remove it first (e.g. myObject.MyEvent -= new EventHandler(this.eventhandler) ?no, an eventhandler does not keep the object alive |
|
You can use WinDbg to find out what objects are filling your heap. There used to be free profiler that showed you the usage of the heap without such a steep lerning curve (compared to using windbg), but I can't seem to find it atm, your search engine of choice might help you. |
#4
| |||
| |||
|
|
An object will only be 'removed' by the GC if it is no longer referenced by anything. If I have wired up one of that object's events to an event handler do I need to remove it first (e.g. myObject.MyEvent -= new EventHandler(this.eventhandler) ?no, an eventhandler does not keep the object alive this is contrary to my experience (and contrary to http://diditwith.net/2007/03/23/ SolvingTheProblemWithEventsWeakEventHandlers.aspx) . If I have an object A which points to object B via an eventhandler, object B is not discarded until object A is also collected. |
#5
| |||
| |||
|
|
If I have an object A which points to object B via an eventhandler, object B is not discarded until object A is also collected. |
#6
| |||
| |||
|
|
GreatObject foo = new GreatObject(); foo.SomeEvent += new EventHander(bla); |
#7
| |||
| |||
|
|
GreatObject foo = new GreatObject(); foo.SomeEvent += new EventHander(bla); In this case, foo has a strong reference to the object of the bla method. So in this case, instance which "hosts" the bla method is kept alive until foo goes out of scope. |
#8
| |||
| |||
|
|
On Thu, 20 Sep 2007 18:42:03 +0200, Henning Krause [MVP - Exchange] wrote: GreatObject foo = new GreatObject(); foo.SomeEvent += new EventHander(bla); In this case, foo has a strong reference to the object of the bla method. So in this case, instance which "hosts" the bla method is kept alive until foo goes out of scope. Yes, and any other behaviour would be *very* counter inintuitive! Ben |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |