Skip to main content
GameDev.net gamedev.net
🔒 Locked

delete a window

Started by Link Jan 11, 2004 at 5:56 AM 7 replies 350+ views
Original Post
Link
Link
//i make a window in this way: Form2* createNewWindow = new Form2(); //but when i call delete...: delete createNewWindow; createNewWindow = NULL; i get this error: c:\...\Form1.h(111): error C3841: illegal delete expression: managed type ''MyApplication::Form2'' does not have a destructor defined
SiCrane
SiCrane
Calling delete on managed objects only implements finalization. If no destructor exists for an object, which means that there''s no Finalize() member function, then it is an error to call delete on that object.
Link
Link
so, how can i free the memory?
SiCrane
SiCrane
Managed objects are garbage collected. That''s part of the point.
Link
Link
so when the user close the window it is automatically free the memory
Ruudje
Ruudje
probably only after your program is shut down, as there is a difference between closing a window and destroying it... (though im not a .net programmer so correct me if im wrong plz)
SiCrane
SiCrane
During the next garbage collection cycle if all references to a managed object are dead then the memory for the object will be reclaimed.
Link
Link
what means garbage collection?
Zahlman
Zahlman
http://c2.com/cgi/wiki?GarbageCollection

I will give my own description here, too.

Garbage collection is the thing that Java has been doing for years (and Smalltalk for decades) that many people feel is needed for doing proper OO development. It means that the program has a separate thread run which detects objects that are no longer usable and reclaims the memory for you.

In Java there is no equivalent to free() because it would be meaningless. The closest you get is finalize(), which is a method called on an object (from the garbage-collecting thread) right before the memory is freed up.

In C# or whatever it is that you''re using, there is apparently the ability to tag objects as "managed", and some library classes (including Form2) are thus tagged. (I don''t know anything about this new-fangled stuff and I''m picking it all up as I go along, but since it''s all in the C family of languages it''s not too hard for me to understand what''s going on) This means that it''s added to the set of objects that the garbage collector knows about. Trying to delete one of these objects yourself is an error because you''re getting in the GC''s way.

With a non-managed object, you get more power, i.e. control over when the delete actually happens. The garbage collector runs periodically, and not under your control (in Java, you can *suggest* that it run, or pause every other thread, but you still don''t really have the same control); each time it runs, it frees up everything that happens to be ready - it may have been ready for a long time. When you''re managing the objects, you can detect the instant the last reference is gone, and handle it right away as part of the reference-removal procedure. But With Power Comes Responsibility; you have to *do that work*, otherwise your memory leaks. For some data structures (particularly ones containing cycles) this becomes a very hard task, that many Smalltalk purists etc. believe the programmer ought never be burdened with. Eh, your call really.

So you''re probably wondering by now how the process *works* - the link at the top gives better information than I can here, but basically the axiom is that an object is "dead" and should be freed, if and only if

1) no "live" object references it; and
2) it is not intrincially "live" by virtue of being on the stack or referenced within a register, or some other special provision.

So the basic idea is to do graph traversal from the "automatically live" stuff, and trash everything you don''t reach. There are a couple ways to do that.

(Those of you who like to get into the theory of this stuff might also be interested in http://c2.com/cgi/wiki?ReferenceCountingCanHandleCycles - a page I wrote, actually Looks like noone''s made any serious edits to it either, so yeah.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.