Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!mips!apple!lsr From: lsr@Apple.COM (Larry Rosenstein) Newsgroups: comp.sys.mac.programmer Subject: Re: MacApp & C++ destructors Message-ID: <13078@goofy.Apple.COM> Date: 16 Apr 91 00:19:55 GMT References: <1991Apr10.210516.25812@rice.edu> <9827@etsu.CMI.COM> <1991Apr13.235903.26110@ux1.cso.uiuc.edu> Organization: Apple Computer, Inc. Lines: 35 In article <1991Apr13.235903.26110@ux1.cso.uiuc.edu> mlanett@yoyodyne.ncsa.uiuc.edu (Mark Lanett) writes: >Has anyone tried using destructors in objects descended from TObject? The >destructors appear not to get called. This is very bizarre since the The problem might be that the destructor isn't declared as virtual. If the destructor is not virtual, then 'delete obj', where obj is declared as TObject* will try to call TObject's destructor, which doesn't exist. On the other hand, I'm not sure if you can declare a virtual destructor in a subclass of PascalObject. There's also a problem with doing this if your object descends from a MacApp class. MacApp classes assume that Free() is going to be called and do their clean up there, so Free() has to be called at some point. But TObject.Free() also deletes the storage for the object. The same is true of the statement 'delete obj'. It not only calls the destructor but also deletes the storage for the object. So either Free() doesn't get called (and superclasses don't get to clean up), or the object is deleted twice. You may be able to suppress the normal C++ deletion by overriding operator delete in the class, but I'm not sure that this is legal for a subclass of PascalObject. In general, if you are using subclasses of TObject, it is best to use the MacApp conventions for initializing and deleting them. If you want to use constructors/destructors you should declare a "native" C++ class (eg, a subclass of HandleObject). -- Larry Rosenstein, Apple Computer, Inc. lsr@apple.com (or AppleLink: Rosenstein1)