Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Do i have to remove the Events ?

Do i have to remove the Events ?

Scheduled Pinned Locked Moved C#
question
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarkPhB
    wrote on last edited by
    #1

    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?

    C L M 3 Replies Last reply
    0
    • M MarkPhB

      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?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      M 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        M Offline
        M Offline
        MarkPhB
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M MarkPhB

          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?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0
          • M MarkPhB

            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?

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups