Question

Here is my code:

void autoreleasingReturn (NSError * __autoreleasing *error)
{
    // --- Crashing
    *error = [[NSError alloc] init];

    // --- Not crashing
//    *error = [NSError errorWithDomain:@"hello"
//                                 code:-1
//                             userInfo:@{}];
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSError __strong *error = nil;

        autoreleasingReturn(&error);

        NSLog(@"error: %@", error); // crash!
    }

    return 0;
}

Traces (sorry, can not copy-paste):

enter image description here

The question is: what is going on?

I need details on what is going on in this code. Really interesting... Instruments don't show any leaks, zombies etc.

Was it helpful?

Solution

initWithDomain:code:userInfo: is the designated initializer for NSError. That means don't call plain init.

You would get the same crash just by saying this:

NSLog(@"%@", [[NSError alloc] init]);

The problem has nothing to do with automatic ref counting, autorelease, autorelease pool, strong, or any of that other stuff! It's just that you are making a bad NSError object and then trying to log it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top