Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!att!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: Eiffel vs. C++ -- Let's drop the garbage collection arguments Summary: total cleanup requires ``automatic destruction,'' not the reverse Message-ID: <9525@alice.UUCP> Date: 23 Jun 89 16:00:21 GMT References: <6590166@hplsla.HP.COM> <9522@alice.UUCP> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 40 In article <9522@alice.UUCP>, bs@alice.UUCP (Bjarne Stroustrup) writes: > There are therefore two possible models for garbage collection of C++: > > (1) the `infinite memory' model where destructors are only called > for explicitly deleted objects, and > (2) the `automatic destruction' model where destructors are called > where the memory used to store objects are recycled. > > The former is more `manual' and more predictable in terms of when things > happen. The latter more automatic and (I think) imposes the rule that every > object on the free store must be destroyed on normal program termination. This seems backwards. If the semantics of your class requires that every object which is created be destroyed before program termination, then objects that remain in the free store at program termination must be destroyed and objects that are reclaimed by GC must also be destroyed. [The former can be arranged pretty easily by chaining all objects of the class together and appointing a static object (of another class) to destroy anything left on the chain when it is destroyed.] If the semantics of the class allows objects to be simply abandoned when they will no longer be used then they can be left in the free store when the program terminates and GC can recycle the object's memory when the object is inaccessible. An example of a class whose semnantics require that every object be destroyed is a file class where the destructor flushes an internal buffer, and an example of a class whose semantics allow objects to be abandoned is a list whose destructor deletes the elements of the list. The `automatic destruction' model is suitable for both kinds of classes. If you have an `infinite memory' model GC and you have a class whose objects cannot be simply abandoned, you must be sure not to lose any of them. I actually don't think this is a very heavy requirement, because (I think) such classes are not very common, and when they are used, not many instances are created. The heavy requirement is, as usual, you have to know what you are doing. -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229