Search code examples
iphoneobjective-ciosexceptionreference-counting

ios @try @catch block EXC_BAD_ACCESS on return from @catch


This @try-@catch block in my viewDidLoad crashes with EXC_BAD_ACCESS when the return; is executed in the catch and the alert doesn't show either:

    @try
    {        
        errorText = @"thumbnails_array";

        unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        // Customize unarchiver here
        self.thumbnails_array = [unarchiver decodeObjectForKey:@"thumbnails_array"];
        [unarchiver finishDecoding];
        [unarchiver release];


        errorText = @"ThumbNailViewController";

        archivePath = [app.phojoArchiveDir stringByAppendingPathComponent:@"ThumbNailViewController.archive"];
        data = [NSData dataWithContentsOfFile:archivePath];
        unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        // Customize unarchiver here
        [unarchiver decodeObjectForKey:@"self"];
        [unarchiver finishDecoding];
        [unarchiver release];

        errorText = @"assetsGroupURL";

        archivePath = [app.phojoArchiveDir stringByAppendingPathComponent:@"assetsGroupURL.archive"];
        data = [NSData dataWithContentsOfFile:archivePath];
        unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        // Customize unarchiver here
        app.assetsGroupURL = [unarchiver decodeObjectForKey:@"assetsGroupURL"];
        [unarchiver finishDecoding];
        [unarchiver release];


    }
    @catch (NSException *exception) 
    {
       UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Phojo is unable to restore the previous editing session." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];

        NSLog(@"Exception %@ thrown while unarchving %@: Reason: %@ Items in userInfo = %d Stack Trace: %@", [exception name], errorText, [exception reason], [[exception userInfo] count], [NSThread callStackSymbols]);  
        [self.thumbnails_array   release];
        self.thumbnails_array = nil;
        [app.assetsGroupURL release];
        app.assetsGroupURL = nil;

        return;


    }

This code is run in the viewDidLoad to retrieve data that have been archived during a previous run of the app. I've gotten an exception in this code stating that an archive is uncomprehensible. But with it crashing there is no way to get the app to run at all since it crashes on startup as well on the catch. Any ideas?


Solution

  • Either your assetsGroupURL or thumbnails_array properties (or both) are declared as retain. That’s fine, but it means that when you call both [self.theProperty release] and self.theProperty = nil, you’re releasing theProperty twice: the second call is using the retain-generated setter and implicitly calling release on its current value as well. Remove the release calls and you should no longer see the EXC_BAD_ACCESS.