【问题标题】:release after pushing view onto navigation stack gives me exc_bad_access error将视图推送到导航堆栈后释放给我 exc_bad_access 错误
【发布时间】:2011-05-16 00:25:30
【问题描述】:

以下代码在 tableView didSelectRowAtIndexPath 中,如果版本未注释我得到错误:

ComicDetailsViewController * comicDetailsViewController = [[ComicDetailsViewController alloc] initWithNibName:@"ComicDetailsViewController" bundle:nil];
       comicDetailsViewController.comic = (Comic *)[arrayOfComics objectAtIndex:indexPath.row];
       comicDetailsViewController.bLoadPerformances = YES;
       [self.navigationController pushViewController:comicDetailsViewController animated:YES];
       //[comicDetailsViewController release];

错误不会立即发生,它会在我单击 omicDetailsViewController 中的后退按钮时发生。

ie.) 我选择 tableview 行,下一个视图加载并正常工作。一旦我完成了该视图并单击后退导航按钮,程序就会崩溃并给我 exc_bad_access。这是为什么呢?

编辑:

#0  0x9682b176 in __kill
#1  0x9682b168 in kill$UNIX2003
#2  0x968bd89d in raise
#3  0x968d39bc in abort
#4  0x968c2164 in szone_error
#5  0x968c21e7 in free_small_botch
#6  0x000a7877 in -[NSConcreteMutableData dealloc]
#7  0x00006433 in -[ComicDetailsViewController dealloc] at ComicDetailsViewController.m:376
#8  0x003cbcc7 in -[UINavigationController setDisappearingViewController:]
#9  0x003c9219 in -[UINavigationController _clearLastOperation]
#10 0x003c9b62 in -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:]
#11 0x0055224a in -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:]
#12 0x0055338a in -[UINavigationTransitionView _navigationTransitionDidStop]
#13 0x0034829d in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
#14 0x0034812f in -[UIViewAnimationState animationDidStop:finished:]
#15 0x0244ca28 in run_animation_callbacks
#16 0x0244c8e9 in CA::timer_callback
#17 0x02688d43 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__
#18 0x0268a384 in __CFRunLoopDoTimer
#19 0x025e6d09 in __CFRunLoopRun
#20 0x025e6280 in CFRunLoopRunSpecific
#21 0x025e61a1 in CFRunLoopRunInMode
#22 0x02f0c2c8 in GSEventRunModal
#23 0x02f0c38d in GSEventRun
#24 0x00326b58 in UIApplicationMain
#25 0x000020f4 in main at main.m:14

编辑 2:

这里是comicDetailsViewController 的dealloc 块:

- (void)dealloc {
[comic release];
[xmlParser release];
[webData release];
[currentPerformanceObject release];
[arrayOfPerformances release];
[soapResults release];
[btnPerformances release];
[super dealloc];

}

第 376 行是 webData 发布行

编辑 3:

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
    [connection release];
    [webData release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    if(xmlParser)
    {
        [xmlParser release];
    }

    xmlParser = [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];

    [connection release];
    [webData release];
}

【问题讨论】:

  • 嗯,文字很有趣。这就是堆栈跟踪的意思吗?
  • 请在ComicDetailsViewController 课程的第 376 行显示您在做什么
  • 你确定它在第 376 行,因为你写的第 326 行有点混乱
  • 抱歉有点累。打错 326

标签: iphone objective-c uinavigationcontroller pushviewcontroller


【解决方案1】:

请向我们展示堆栈跟踪。

但我最初的猜测是你在 - (void) deallocComicDetailsViewController 类中做错了什么。

请检查几件事

  • 你的dealloc 块没问题
  • 您没有释放 comicDetailsViewController 对象或从其他地方对其的引用。

希望这会有所帮助。

谢谢, 马杜普

【讨论】:

  • 第 376 行在我对原始问题的编辑中,它是 dealloc 块的一部分
  • 请检查网络数据是否有问题。在进行网络呼叫并尝试将结果保存到 webdata 时,您是否在按下回键?这就是我的猜测,您可能正在做一些不同的事情。
  • 你是对的,我正在运行异步 NSURLConnection,但是根据代码 didFinishConnection 已经运行并且 NSXMLParser 委托。我不确定为什么 WebData 仍在使用中......
  • 宾果游戏!!!尝试取消所有网络调用并重新加载您的视图。这将解决问题。
  • 我不确定如何取消网络调用,当我点击返回导航按钮时会发生此错误。我需要什么活动?
【解决方案2】:

在弹出视图之前,通过向适当的对象发送取消消息来取消您的 NSURLConnection。

假设你的 NSURLConnection 对象被命名为 asyncConnection:

-(void)viewWillDisappear:(BOOL)animated
   [asyncConnection cancel];
}

【讨论】:

  • 我添加了这个,它在取消行 exc_bad_access 上崩溃。
  • 如果在 didFailWithError 委托和 connectionDidFinishLoading 事件中,我将释放连接。
【解决方案3】:

问题仍然可能在你的dealloc中。

仅仅因为对象在您的@interface(.h 文件)中,这并不意味着您可以/必须释放变量。我也遇到过这个问题

.h 文件

@interface PeopleViewController : UITableViewController {
NSArray *people;
}

@property (nonatomic, retain) NSArray *people;

@end

.m 文件

@implementation PeopleViewController

@synthesize people;

- (void)viewDidLoad {
    people = [town.people allObjects];
}

...

- (void)dealloc {
    [people release];
}

@end

我不需要释放人员,因为在自定义视图中时我没有增加对象的保留计数,因此在 dealloc 过程中释放它会导致我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多