从@catch返回ios @try @catch阻止EXC_BAD_ACCESS

时间:2012-02-10 17:20:40

标签: iphone objective-c ios exception-handling reference-counting

我的viewDidLoad中的@ try- @ catch块在返回时与EXC_BAD_ACCESS崩溃;在catch中执行,警报也不显示:

    @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;


    }

此代码在viewDidLoad中运行,以检索在上一次运行应用期间归档的数据。我在这段代码中得到了一个例外,说明档案是不可理解的。但随着它的崩溃,没有办法让应用程序运行,因为它在启动时也会崩溃。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您的assetsGroupURLthumbnails_array属性(或两者)都被声明为retain。这很好,但这意味着当你同时调用[self.theProperty release]self.theProperty = nil时,你会释放theProperty两次:第二次调用是使用retain - 生成的setter并隐式同时调用release的当前值。删除release电话,您将不再看到EXC_BAD_ACCESS。