On May 19, 2005, at 5:17 AM, Theodore H. Smith wrote: C++ destructors are not called on C++ class in a ObjC class field, when the ObjC class is dealloced.
Any strategies for dealing with this?
I've thought of making one C++ class which contains all the C++ stuff, and just new/delete a pointer to that class from my ObjC init/delloc methods.
In gcc 4.0 (Xcode 2.0 and later), try setting the following in your OTHER_CFLAGS:
-fobjc-call-cxx-cdtors For each Objective-C class, check if any of its instance variables is a C++ object with a non-trivial default constructor. If so, synthesize a special - (id) .cxx_construct instance method that will run non-trivial default constructors on any such instance variables, in order, and then return self. Similarly, check if any instance variable is a C++ object with a non-trivial destructor, and if so, synthesize a special - (void) .cxx_destruct method that will run all such default destructors, in reverse order.
The - (id) .cxx_construct and/or - (void) .cxx_destruct methods thusly generated will only operate on instance variables declared in the current Objective-C class, and not those inherited from superclasses. It is the responsibility of the Objective-C runtime to invoke all such methods in an object's inheritance hierarchy. The - (id) .cxx_construct methods will be invoked by the runtime immediately after a new object instance is allocated; the - (void) .cxx_destruct methods will be invoked immediately before the runtime deallocates an object instance.
As of this writing, only the NeXT runtime on Mac OS X 10.4 and later has support for invoking the - (id) .cxx_construct and - (void) .cxx_destruct methods.
Chris |