23

I used the answer of this question, List selectors for Objective-C object and list out all the selectors to which my class object is responding. In a huge list I found a selector named ".cxx_destruct" (yes, it's start with a dot), I am seeing this for the first time, and never heard about it too. I googled for it and found this Objective C: ARC errors (Automatic release problems).

I've some questions in my mind?

  • Is it related to ARC?
  • If its a selector why its hidden?
  • When it'll call? By whom, an iOS itself?
  • Why its named like .cxx_destruct? what's the full form of "cxx".. ?
Community
  • 1
  • 1
Hemang
  • 25,740
  • 17
  • 113
  • 171
  • Yes. Because it's related to ARC and shouldn't be invoked manually. When your object is deallocated. By the objective C runtime. The naming pattern is because it's related to destructors for c++. – Petesh Jun 09 '14 at 13:11
  • @Petesh, Can you reference me to the Apple Doc for this? – Hemang Jun 09 '14 at 13:12
  • I don't have a reference to this; It's more compiler-level rather than simply Apple specific. The compiler emits a `.cxx_destructor` automatically when you have object-type instance variables in order to ensure that they get dereferenced automatically on destruction. This is the reason you don't need to write a dealloc routine to null-out all the properties. It's got the .cxx_destructor title because it's leveraging objective-c++ destructor behaviour. – Petesh Jun 09 '14 at 13:23
  • @Petesh, Thanks for you good efforts, please make your comments an answer, so I would accept it. – Hemang Jun 09 '14 at 13:26
  • we compile without ARC (clang -fno-objc-arc test.m -o test -framework foundation), – Yan.Zhao Jan 15 '19 at 03:04

1 Answers1

26

Prior to ARC, developers would have to deliberately write a dealloc routine to ensure that all the references to all the objects they retained were released. This was manual and error-prone work.

When ARC was introduced code that performs an equivalent task as these manual releases would have to be implemented in each object that possessed anything more than simple properties. Relying on a developer manually implementing a dealloc routine would defeat this.

Note: This is just for the purposes of reference count management when destroying objects. If you need to remove observers or perform other cleanup work, then you'll still need a dealloc routine.

As a result, the pre-existing mechanism from objective-c++ was used, namely a hidden selector called .cxx_destruct which is invoked automatically just prior to the object being deallocated. This selector is invoked automatically by the Objective C run-time.

For Objective C++ code, there is a parallel .cxx_construct for construction.

Again, these are automatically generated by the compiler to deal with object destruction in the ARC context. You can see it being created if you compile some simple objective C code with and without an object property. Take this sample code:

#import <Foundation/Foundation.h>

@interface Foo : NSObject

@property (strong) NSObject *anobject;

@end

@implementation Foo
@end

int main()
{
    Foo *f = [[Foo alloc] init];
    return 0;
}

When we compile with ARC (clang -fobjc-arc test.m -o test -framework foundation) and dump the class information we see a .cxx_destruct selector, when we compile without ARC (clang -fnoobjc-arc test.m -o test -framework foundation), we do not see a .cxx_destruct selector. If you comment out the NSObject *anobject property and recompile, you will not see the .cxx_destruct as it is no longer needed.

Petesh
  • 82,900
  • 3
  • 91
  • 111
  • Great answer! How to avoid Xcode to include it in code coverage reports? – SalvoC Aug 12 '15 at 15:37
  • I don't know that - I wouldn't have expected them to appear in typical coverage reports as they're not mapped to any source-code lines, though. – Petesh Aug 12 '15 at 16:34
  • This answer looks like it's also trying to say that you shouldn't implement `dealloc` under `ARC`, but that's [not true](http://stackoverflow.com/a/7292157/865175). – Iulian Onofrei Sep 05 '16 at 10:57
  • It was only meant in the context of releasing references. Other cleanup work would still need to be implemented in a `dealloc` routine. I'll add a note in the answer about that. – Petesh Sep 05 '16 at 12:23