Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!samsung!think.com!barmar From: barmar@think.com (Barry Margolin) Newsgroups: comp.lang.clos Subject: Re: destructors in CLOS? Message-ID: <1990Dec19.202733.29288@Think.COM> Date: 19 Dec 90 20:27:33 GMT References: Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 36 In article gumby@Cygnus.COM (David Vinayak Wallace) writes: > From: creon@nas.nasa.gov (Creon C. Levit) > > I would like to define a method that gets called when a CLOS instance > gets garbage collected, sort of like a destructor method in c++. >What do you want to happen? For instance, an object may never BE >collected, since the GC may never run. That's a good point. I expect that an example of what he's doing is objects containing references to streams. If the only reference to a stream is in a particular instance, and the instance gets GCed, you would like to know to close the stream. The design of many modern GCs doesn't make such a feature easy to implement. Many systems use some form of copying GC, so the only objects it sees are the live ones; the garbage gets left in oldspace, which is then reclaimed in one fell swoop. Searching through oldspace for all the garbage instances would significantly slow down the GC; this would end up as a hybrid of copying and mark-sweep (copy-and-sweep?). In my experience, most objects that need this kind of special treatment are amenable to solutions using explicit destruction (e.g. the CLOSE function) and/or automatic destruction when leaving a dynamic environment (e.g. the WITH-OPEN-FILE macro). Not surprisingly, these are the same operations that invoke C++ destructors: using the "delete" operator on a pointer returned by "new", and leaving a block that declared an automatic class object. In addition, Lisp generally allows you to invoke an explicit destructor on an object allocated by a dynamic macro, which I'm not sure is valid in C++. -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar