Do i have to remove the Events ?
-
Hi, Lets say i have an objekt with events ...
class TestClass { public event EventHandler TestEvent1; public event EventHandler TestEvent2; ... public TestClass() {} }
and now i have one Main Form the works with that object...
class MainForm : Form { TestClass testObj; public MainForm() { testObj.TestEvent1 += new EventHandler(testObj_TestEvent1); testObj.TestEvent2 += new EventHandler(testObj_TestEvent2); } void testObj_TestEvent1 (object sender, EventArgs e){} void testObj_TestEvent2 (object sender, EventArgs e){} }
and i have a second form the works with the object too, but not mainly. Thats why i start the second form with the instance of the object...
class SecondForm: Form { TestClass testObj; public SecondForm(TestClass obj) { if(obj == null){ throw new ArgumentNullException(); } testObj = obj; testObj.TestEvent1 += new EventHandler(testObj_TestEvent1); testObj.TestEvent2 += new EventHandler(testObj_TestEvent2); } void testObj_TestEvent1 (object sender, EventArgs e){} void testObj_TestEvent2 (object sender, EventArgs e){} }
My question is now...is the any thing i have to remove or destroy when i close the second form ? maybe ...remove the added events from the testObj?
-
Hi, Lets say i have an objekt with events ...
class TestClass { public event EventHandler TestEvent1; public event EventHandler TestEvent2; ... public TestClass() {} }
and now i have one Main Form the works with that object...
class MainForm : Form { TestClass testObj; public MainForm() { testObj.TestEvent1 += new EventHandler(testObj_TestEvent1); testObj.TestEvent2 += new EventHandler(testObj_TestEvent2); } void testObj_TestEvent1 (object sender, EventArgs e){} void testObj_TestEvent2 (object sender, EventArgs e){} }
and i have a second form the works with the object too, but not mainly. Thats why i start the second form with the instance of the object...
class SecondForm: Form { TestClass testObj; public SecondForm(TestClass obj) { if(obj == null){ throw new ArgumentNullException(); } testObj = obj; testObj.TestEvent1 += new EventHandler(testObj_TestEvent1); testObj.TestEvent2 += new EventHandler(testObj_TestEvent2); } void testObj_TestEvent1 (object sender, EventArgs e){} void testObj_TestEvent2 (object sender, EventArgs e){} }
My question is now...is the any thing i have to remove or destroy when i close the second form ? maybe ...remove the added events from the testObj?
testObj.TestEvent1 -= new EventHandler(testObj_TestEvent1); testObj.TestEvent2 -= new EventHandler(testObj_TestEvent2); Of course, it still fires in your main class, because you don't remove it there, an event is a chain, not a single call.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
testObj.TestEvent1 -= new EventHandler(testObj_TestEvent1); testObj.TestEvent2 -= new EventHandler(testObj_TestEvent2); Of course, it still fires in your main class, because you don't remove it there, an event is a chain, not a single call.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
I dont use it like this, the MainForm keep the object and give it to the SecondForm. Because the SecondForm use other methods an events of the object than the MainForm does. The SecondForm close it selft when its done with its work . OK, i should remove the added events from the SecondForm. But i didnt done this befor and got no execption wich would indicate, that i should do remove the added events. :confused: Is it possible that the GC done that for me ? -- modified at 7:48 Friday 22nd June, 2007
-
Hi, Lets say i have an objekt with events ...
class TestClass { public event EventHandler TestEvent1; public event EventHandler TestEvent2; ... public TestClass() {} }
and now i have one Main Form the works with that object...
class MainForm : Form { TestClass testObj; public MainForm() { testObj.TestEvent1 += new EventHandler(testObj_TestEvent1); testObj.TestEvent2 += new EventHandler(testObj_TestEvent2); } void testObj_TestEvent1 (object sender, EventArgs e){} void testObj_TestEvent2 (object sender, EventArgs e){} }
and i have a second form the works with the object too, but not mainly. Thats why i start the second form with the instance of the object...
class SecondForm: Form { TestClass testObj; public SecondForm(TestClass obj) { if(obj == null){ throw new ArgumentNullException(); } testObj = obj; testObj.TestEvent1 += new EventHandler(testObj_TestEvent1); testObj.TestEvent2 += new EventHandler(testObj_TestEvent2); } void testObj_TestEvent1 (object sender, EventArgs e){} void testObj_TestEvent2 (object sender, EventArgs e){} }
My question is now...is the any thing i have to remove or destroy when i close the second form ? maybe ...remove the added events from the testObj?
Hi, this is how I understand the situation: if SecondForm does not remove the events, then SecondForm will not be garbage collected as long as testObj is alive (which happens to be as long as MainForm is alive). Rationale: SecondForm adds event handlers to testObj's events; these handlers include a "this" reference (pointing to SecondForm) since they call a method (inside SecondForm) on the specific instance of SecondForm. Therefore your instance of SecondForm is kept alive by testObj; closing the SecondForm is irrelevant; Disposing it should lead to an ObjectDisposedException whenever one of the testObj's events fires (again). Conclusion: forgetting to remove event handlers can result in keeping a lot of memory both occupied (i.e. alive) and useless (a form that has been closed isnt very useful anymore). It resembles a memory leak. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, Lets say i have an objekt with events ...
class TestClass { public event EventHandler TestEvent1; public event EventHandler TestEvent2; ... public TestClass() {} }
and now i have one Main Form the works with that object...
class MainForm : Form { TestClass testObj; public MainForm() { testObj.TestEvent1 += new EventHandler(testObj_TestEvent1); testObj.TestEvent2 += new EventHandler(testObj_TestEvent2); } void testObj_TestEvent1 (object sender, EventArgs e){} void testObj_TestEvent2 (object sender, EventArgs e){} }
and i have a second form the works with the object too, but not mainly. Thats why i start the second form with the instance of the object...
class SecondForm: Form { TestClass testObj; public SecondForm(TestClass obj) { if(obj == null){ throw new ArgumentNullException(); } testObj = obj; testObj.TestEvent1 += new EventHandler(testObj_TestEvent1); testObj.TestEvent2 += new EventHandler(testObj_TestEvent2); } void testObj_TestEvent1 (object sender, EventArgs e){} void testObj_TestEvent2 (object sender, EventArgs e){} }
My question is now...is the any thing i have to remove or destroy when i close the second form ? maybe ...remove the added events from the testObj?
Hello, Additionaly to what Christian and Luc where telling you (which was 100% correct of course), I made the expirience that you also have to set the "testObj" to 'null'. I would suggest to override the Dispose method of the SecondForm, where you remove the delegates and set it null. All the best, Martin