Click here to Skip to main content
15,884,986 members
Articles / Mobile Apps / Windows Phone 7

Why you need to remove event handler for XNA?

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
26 Jul 2012CPOL1 min read 7.3K   2   1
Why you need to remove event handler for XNA?

Title appears to be a nonsense

Okay, when you added the event, you need to remove them. Let’s look at these code below:

private void quitted ( object sender, EventArgs e )
{ Console.Write ( "I quit" );  }

Student tom = new Student ( );
tom.Quitted += new EventHandler ( this.quitted );

tom.Quitted -= new EventHandler ( this.quitted );

We added a Quitted event for student, and then, we remove this event. If the instance tom do not remove Quitted event, what will happen? To be honest, I don’t know, but one thing to determine is if you have a lot of tom like this, then, it will result memory leak, eventually leads to a exception.

WinForm

If it is a WinForm program, you added a Click event for a button, you don’t need to remove it when the window is destroyed, I think perhaps is for the convenience of the programmers, these this job already performed by .NET or Visual Studio.

XNA

XNA is actually simple, and a lot of work you need to finish them by yourself, especially the events of Sprite in the game, because the game itself is constituted by a large number of the Sprites, they are constantly created and destroyed.So, if you do not remove events of Sprites, the game will be over (not because the player is dead, but game crashes).

I actually ran into this problem (OK, I was stupid), no matter how to try, the game always throws a exception for a limited period of time.Fortunately, friends reminded me that I should remove the events, the problem is finally solved.

Make sure that the code is called

If you added event for an instance in a method of a class, then in the other methods in this class, you should remove the event for this instance, and then make sure the method is called.

private Student one = new Student ( );

public School ( )
{ this.one.Quitted += ...; }

public void End ( )
{ this.one.Quitted -= ...; }
This article was originally posted at http://mscxc.tumblr.com/post/28039558658

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
ely_bob26-Jul-12 6:39
professionalely_bob26-Jul-12 6:39 
XNA and event driven architecture is not recommended.. but even so this post is hard to follow and the code samples are not straightforward, i.e misleading..and incomplete.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.