在UITableView委托方法中访问实例变量时获取“EXC_BAD_ACCESS” [英] Getting “EXC_BAD_ACCESS” when accessing instance variable from within UITableView delegate method

查看:151
本文介绍了在UITableView委托方法中访问实例变量时获取“EXC_BAD_ACCESS”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的iPhone应用程序从一个UITableView委托方法中访问一个实例变量时,我的iPhone应用程序就会爆炸。我想我保留它,所以我不明白为什么我无法访问它没有问题。



这是我的.h文件

  #import< Foundation / Foundation.h> 
#importAlertSummaryCell.h
#importAlertDetailViewController.h

@interface AlertSummaryTableViewController:UITableViewController {
NSDictionary * alerts;
NSString * alertKind;
}

@property(nonatomic,retain)NSDictionary * alerts;
@property(nonatomic,retain)NSString * alertKind;



@end



在我的m,应用程序在第一次NSLog调用时死机:

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger )section {
NSLog(@AlertSummaryTableViewController.numberOfRowsInSection entered);
NSLog(@alerts description =%@,[alerts description]);
//要知道我们有多少个警报摘要,请从输入字典获取计数值
int theCount = [[alerts objectForKey:@count] intValue];
NSLog(@要返回%d,theCount);
返回theCount;
}

我缺少什么?



viewDidLoad方法中没有任何问题:

   - (void)viewDidLoad {
NSLog(@AlertSummaryTableViewController.viewDidLoad);
NSLog(@alerts description =%@,[alerts description]);

//我们希望查看标题包含警报计数
//要知道有多少警报摘要,请从输入字典获取计数值
int theCount = [[alerts objectForKey:@count] intValue];

//现在构造标题文本并设置我们的视图标题
NSString * myTitle = [[NSString alloc] initWithFormat:@%@ Alerts(%d),alertKind, theCount];
[self setTitle:myTitle];

//内存清理
[myTitle release];

[super viewDidLoad];
}


解决方案

对于任何EXC_BAD_ACCESS错误通常会尝试向发布的对象发送消息。追踪这些情况的 BEST 方式是使用 NSZombieEnabled



这个工作从不实际释放一个对象,而是将其包装成僵尸,并在其中设置一个通常会被释放的标志。这样,如果您尝试再次访问它,它仍然会知道您在发生错误之前是什么,并且通过这一点信息,您通常可以回溯查看问题。



当调试器有时候会抛出任何有用的信息时,它特别有助于后台线程。



非常重要的注意事项但是,您需要100%确保这仅在您的调试代码中,而不是您的分发代码。因为没有任何东西被释放,你的应用程序会泄漏和泄漏。要提醒我这样做,我把这个日志写在我的app_范例:

  if(getenv(NSZombieEnabled)|| getenv (NSAutoreleaseFreedObjectCheckEnabled))
NSLog(@NSZombieEnabled / NSAutoreleaseFreedObjectCheckEnabled enabled!);


My iPhone application blows up when it accesses an instance variable from within one of the UITableView delegate methods. I think I'm retaining it so I do not understand why I can't access it without a problem.

Here's my .h file

#import <Foundation/Foundation.h>
#import "AlertSummaryCell.h"
#import "AlertDetailViewController.h"

@interface AlertSummaryTableViewController : UITableViewController {
NSDictionary *alerts;
NSString *alertKind;
}

@property (nonatomic, retain) NSDictionary *alerts; @property (nonatomic, retain) NSString *alertKind;

@end

In my .m, the application dies at the first NSLog call:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"AlertSummaryTableViewController.numberOfRowsInSection entered");
NSLog(@"  alerts description = %@", [alerts description]);
// To know how many alert summaries we have, get the value for count out of the input dictionary
int theCount = [[alerts objectForKey:@"count"] intValue];
NSLog(@"  Going to return %d",theCount);
return theCount;
}

What am I missing???

There's no problem at all in the viewDidLoad method:

 - (void)viewDidLoad {
 NSLog(@"AlertSummaryTableViewController.viewDidLoad entered");
 NSLog(@"  alerts description = %@", [alerts description]);

 // We want the View title to include the alert count
 // To know how many alert summaries we have, get the value for count out of the input dictionary
 int theCount = [[alerts objectForKey:@"count"] intValue];

 // Now construct the title text and set our views title to it
 NSString *myTitle = [[NSString alloc] initWithFormat:@"%@ Alerts (%d)",alertKind,theCount];
 [self setTitle: myTitle];

 // Memory cleanup
 [myTitle release];

 [super viewDidLoad];
 }

解决方案

For any EXC_BAD_ACCESS errors, you are usually trying to send a message to a released object. The BEST way to track these down is use NSZombieEnabled.

This works by never actually releasing an object, but by wrapping it up as a "zombie" and setting a flag inside it that says it normally would have been released. This way, if you try to access it again, it still know what it was before you made the error, and with this little bit of information, you can usually backtrack to see what the issue was.

It especially helps in background threads when the Debugger sometimes craps out on any useful information.

VERY IMPORTANT TO NOTE however, is that you need to 100% make sure this is only in your debug code and not your distribution code. Because nothing is ever release, your app will leak and leak and leak. To remind me to do this, I put this log in my appdelegate:

if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled"))
  NSLog(@"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");

这篇关于在UITableView委托方法中访问实例变量时获取“EXC_BAD_ACCESS”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆