EXEC_BAD_ACCESS in UITableView cellForRowAtIndexPath

白昼怎懂夜的黑 提交于 2019-12-23 10:55:30

问题


My UITableView is returning EXEC_BAD_ACCESS, but why!

See this code snippet!

Loading the UITableView works fine, so allXYZArray != nil and is populated!

Then scrolling the tableview to the bottom and back up causes it to crash, as it goes to reload the method cellForRowAtIndexPath

It fails on line:

    "NSLog(@"allXYZArray::count: %i", [allXYZArray count]);"

        (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAt

IndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];


cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
@try
{ 
if (allXYZArray == nil) {
   NSLog(@"nil");
   allXYZArray = [ToolBox getMergedSortedDictionaries:allXYZGiven SecondDictionary:allXYZSought];
}
NSLog(@"%i", [indexPath row]);
NSLog(@"allXYZArray::count: %i", [allXYZArray count]);

回答1:


EXC_BAD_ACCESS means that your program is trying to access a memory address that is invalid or otherwise inaccessible from your process. This most commonly happens when you try send a message to an object that has already been dealloced. So the first step in debugging EXC_BAD_ACCESS is to figure out which object your program was trying to send a message to when the crash happened. Often the answer isn't obvious, in which case, NSZombieEnabled is a great tool for identifying which line of code caused the crash.

In your case you've already determined that the crash happens when you call [allXYZArray count], making allXYZArray our prime suspect. This object is being returned from +[ToolBox getMergedSortedDictionaries:SecondDictionary:], so it's likely that your bug is in the implementation of that method. I would guess that it's returning an object that has already been released instead of autoreleased, as prescribed by the Memory Management Programming Guide for Cocoa. (This is one of the most important documents in the SDK, by the way. I recommend rereading it once a month until its policies and techniques become second nature.)




回答2:


Ok, reusing a cell, does not guarantee that the cell will be initialized properly:

UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell will sometimes be null (especially the first time I guess).

Check cell for null and if so, initialize it properly.

if (cell == nil)
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


来源:https://stackoverflow.com/questions/2734413/exec-bad-access-in-uitableview-cellforrowatindexpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!