“EXC_BAD_ACCESS” error when cell begins scrolling back in view?


“EXC_BAD_ACCESS” error when cell begins scrolling back in view?



I've got some (I think) pretty basic code for creating cell content from a data source, and everything works fine when the display loads. However, when I start scrolling around to view other text (up or down) the code fails with 'GDB: Program received signal: "EXEC_BAD_ACCESS"'. Here's the code that fills out the display for the various sections; each section has similar code:

id cell = (UITableViewCell *)[tableView 	dequeueReusableCellWithIdentifier:CellIdentifier ];  titledCell = [[[TitledCell alloc] 		  initWithFrame:CGRectZero 		reuseIdentifier:CellIdentifier 	] autorelease ];  switch (tableSection) {  	case TABLE_SECTION_1: 		if (cell == nil) { 			dataKey = @"a key from data source"; 			dataFromSource = [viewData objectForKey:dataKey];  			titledCell.title.text = dataKey; 			titledCell.contents.text = dataFromSource; 			cell = titledCell; 			break; 		} 	case TABLE_SECTION_2:         	... } return cell; 

As I was following the code, I noticed that the code skips the cell creation when scrolling the cell back into view, because cell != nil. If it skips, that means that cell contains the same contents as the first time it was created, right? Why is giving me trouble?


Adding unique objects to Core Data

1:

Using an existing C++ engine on the iPhone
I usually receive EXEC_BAD_ACCESS when I forreceive to retain any thing. How do I set these break points in ~/.gdbinit? If you've got an autorelased object, it may job the first time through although not job the second time.. Is Macro Better Than UIColor for Setting RGB Color? Run the program with debugging and use Xcode to figure out what line it's crashing on. Facebook iphone connection That will be more helpful than anything else.. Is anyone testing Mono-Touch? [closed]
How to intercept text from the keyboard and format it before it show in the cell?Getting an iPhone app's product name at runtime?

2:

I think the EXEC_BAD_ACCESS might caused by the:.
titledCell.title.text = dataKey; 
titledCell might receive dealloced and when accessing the property there'll be a EXEC_BAD_ACCESS exception.. You must turn on the NSZombieEnabled env virable in: Group & Files -> Extutables -> Your App -> Get Info -> Arguments .

3:

Can't be 100% sure with the code sample you've given although a good guess would be the break statement is within the if block. So it should look like:.
switch( tableSection ) {     case TABLE_SECTION_1:             if( cell == nil ) {                     dataKey = @"a key from data source";                     dataFromSource = [ viewData objectForKey:dataKey ];                      titledCell.title.text = dataKey;                     titledCell.contents.text = dataFromSource;                     cell = titledCell;             }             break;     case TABLE_SECTION_2:             ... } 

4:

Unfortunately, changes have made this question academic, so I can't really tell if any of the answers given are correct. The current code looks a little closer to this: .
 id cell = (UITableViewCell *)[tableView       dequeueReusableCellWithIdentifier:CellIdentifier  ];   if (cell == nil) {       titledCell = [[[TitledCell alloc]                    initWithFrame:CGRectZero                  reuseIdentifier:CellIdentifier            ] autorelease       ];        switch (tableSection) {            case TABLE_SECTION_1:                 dataKey = @"a key from data source";                 dataFromSource = [viewData objectForKey:dataKey];                  titledCell.title.text = dataKey;                 titledCell.contents.text = dataFromSource;                 cell = titledCell;                 break;            case TABLE_SECTION_2:            ...  }  return cell; 
...so this most of the code is now in the "if (cell == nil)" block (which makes more sense), and it works fine. I wish I understood what was wrong, although thanks for your Answers anyway!.


80 out of 100 based on 55 user ratings 630 reviews

@