Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!odi!dlw From: dlw@odi.com (Dan Weinreb) Newsgroups: comp.object Subject: Re: copy collecting GCs and destructors? Message-ID: <1991May20.055952.4991@odi.com> Date: 20 May 91 05:59:52 GMT References: Reply-To: dlw@odi.com Organization: Object Design, Inc. Lines: 40 In-Reply-To: tom@ssd.csd.harris.com's message of 14 May 91 17:13:23 GMT I worked for about ten years developing and maintaining system software in Lisp, in which all deallocation was done automatically by a garbage collector. Objects had the equivalent of C++ constructors (called the ":init" method), but there was no equivalent of C++ destructors. (We didn't make any significant use of the "weak link", "post-gc-action" sort of thing that was mentioned in an earlier reply to your posting.) Generally, I don't think it would have been terribly useful to have a feature that would run a method when an object was collected by the GC, for several reasons: (1) If the object contained references to other objects, those other objects could be garbage too, and might no longer exist. So such a method would be exceedingly unsafe. (2) There's no telling when the GC will actually run. It might not be for hours, in principle. So you'd never know when the action would happen. Nowadays I program in C++, so I can compare the techiques used in the two languages. Consider an automatic object in C++. The destructor is run when the block is exited, either normally or because an exception unwinds the block. (Yes, C++ exceptions aren't in the language yet, but they will be.) In Lisp, there is a construct called unwind-protect that lets you set up the same kind of control structure. Those actions that might be placed in a C++ destructor would often correspond, in Lisp, to actions in the handler part of an unwind-protect. An unwind-protect handler might send a message to an object (a.k.a. call a generic function on an object). Sometimes the unwind-protect with this handler would be packaged up in a Lisp macro. A typical such macro, part of the built-in system, is "with-stream", which opens a stream, executes a body, and then (in the unwind-protect handler) closes the stream. For heap-allocated objects, there isn't anything in Lisp that corresponds to the C++ destructor. Instead, where a C++ programmer would use a "delete" statement, the Lisp programmer would just send some message (a.k.a. call some generic function) that would perform actions analogous to those in the C++ destructor.