Can you eat yourself? Not only a philosophical question. One of the hardest things in programming is an object destroying itself. Such as a GUI menu which processes its exit button. How do you do it cleanly.
Written by MM. Posted at 7:32 am on December 29th, 2009
7 comments.
Post a comment.
Either you can:
1) Make just the GUI menu be managed by another object (most specifically by the one which created it, to keep the code clean)
2) Go the easy route with boost::shared_ptr
3) Call delete yourself and don’t access any more members of the object (hardcore-ay)
4) some other obscure method I’m currently not thinking about
I’m using number 3 currently. I agree it’s hardcore. I keep accessing the objects methods after deletion. Not very coder-friendly but its the most clean and fast way from all the 4.
Ah, I remember the times when I was still coding in C, and had to delete objects. Oh the fun of runtime errors.
Now I just remove all references to the object and it deletes itself. Clean and simple like that.
Except when it doesn’t, because it’s linked to an automatically assigned event listener that I wasn’t aware of, and causes a massive 100 megabyte memory leak and incremental CPU burn.
As for your problem, just delete the menu from the object containing the menu. Parent menu deletes child submenu, parent graphicsmanager/perspective/window/whatever deletes root menu. Of all the pointers in an application that point to the menu, there has to be one which has the most relevance, and can be said to “own” the menu, nay?
By the way, it’s not “eat yourself” but delete yourself. Technically kill yourself. And for that we hunams developed a nice little utility class called cGun.
yeah but after you shot yourself you can’t clean up the mess you made.
What about if the GUI, makes a call to a Gun.
If it isn’t better to hide it. I would create a class which receives messages about deleting windows.
When exit button is pressed event listener sends message to that class which deletes window in the next game loop.
MM.. im disappointed.. with all the movie experience you have how could you not resort to Arnold to solve this problem ( http://www.youtube.com/watch?v=KUYhqUgg-ig )