Search code examples
objective-ccocoaobjective-c++

Destructor in Objective-C++


I have an objective-C++ class which contain some honest C++ object pointers.

When the Obj-C++ class is destroyed does it call dealloc immediately? If so, then is the best way to destroy the C++ class by putting

delete obj

in the dealloc method?


Solution

  • I presume when you say "Obj-C++ class" you mean an Objective-C class that happens to contain some C++ classes.

    Objective-C classes don't call dealloc when they're destroyed; they're destroyed by having the dealloc message sent to them.

    With that bit of pedantry out the way, if your init method instantiates obj then, yes, call delete obj in the dealloc:

    -(void)dealloc {
      delete obj;
      [super dealloc];
    }