【发布时间】:2011-09-03 02:06:32
【问题描述】:
我在表视图控制器的 dealloc 中遇到了 EXC_BAD_ACCESS”崩溃。释放被赋予保留属性的 NSMutableArray 时会发生崩溃。我有第二个 NSMutableArray,它也被赋予了一个保留属性,但它的释放不会导致崩溃。请看下面的代码,看看我是否忽略了一些关于内存管理的东西。谢谢。
在我的头文件中,我有以下内容:
@interface selectSourcesTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *selectedNames;
NSMutableArray *selectedAvailability;
}
@property (retain, nonatomic) NSMutableArray *selectedNames;
@property (retain, nonatomic) NSMutableArray *selectedAvailability;
在我的实现中,我有以下内容:
@implementation selectSourcesTableViewController
@synthesize selectedNames;
@synthesize selectedAvailability;
- (void)viewDidLoad {
NSArray *names = [selectedSourceFileContent objectForKey:@"selectedNames"];
selectedNames = [[NSMutableArray alloc] initWithObjects: nil];
NSArray *availability = [selectedSourceFileContent objectForKey:@"selectedAvailability"];
selectedAvailability = [[NSMutableArray alloc] initWithObjects: nil];
for (int i=0; i < [names count]; i++) {
NSString *aName = [names objectAtIndex:i];
[selectedNames addObject: aName];
NSString *anAvailability = [availability objectAtIndex:i];
[selectedAvailability addObject: anAvailability];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: kCellIdentifier];
...
for (int i=0; i < [selectedNames count]; i++) {
if ([contentForThisRow isEqualToString:[selectedNames objectAtIndex:i]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
- (void)dealloc {
[super dealloc];
[selectedNames release];
[selectedAvailability release];
}
上面显示的代码显示了这两个数组的唯一用途。
所以,当 selectedNames 被释放时没有什么不好的事情发生,但是当 selectedAvailability 被释放时我得到一个 EXC_BAD_ACCESS 崩溃。
最后的观察。启动 xcode 后第一次运行此代码时没有崩溃。此后,每次我重新运行应用程序时它都会崩溃。
有什么想法吗?
【问题讨论】:
标签: objective-c ios memory-management nsmutablearray retain