Note that there are some explanatory texts on larger screens.

plurals
  1. POiphone -message sent to deallocated instance
    text
    copied!<p>hope you can help me with this one… I saw some crazy behavior so far in Objective C but this one makes me suicidal. well, NOT really...</p> <p>It's a addressing deallocated object problem.</p> <p>SCENARIO: I have tableView with RSS feeds loaded which upon selecting the row opens a DetailView and presents feed in a UIWebView. Since I need function of sliding to previous and next feed from within DetailView, I'm initializing DetailView with NSMutableDictionary that holds<br> 1.) NSMutableArray of all (15) feeds at key:@"items" and<br> 2.) NSNumber positionInArray that has the index of a row selected in the TableView. </p> <p>The latter I need so I can achieve swipe to previous and next feed by keeping the value of array index at which current feed can be found. With this value I can load positionInArray-1 or positionInArray+1...</p> <p>In my TableView I have:</p> <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //Call an action only if the pressed cells are not the adMob ones if (indexPath.row != 0) { NSMutableDictionary *theItem = [[NSMutableDictionary alloc] init]; [theItem setValue:items forKey:@"items"]; //Add the position of item in the items array to the NSDictionary for current item so it can be //used in nextController to scroll to prev and next NSNumber *positionInArray = [NSNumber numberWithInt:indexPath.row-1]; [theItem setValue:positionInArray forKey:@"positionInArray"]; DetailController *nextController = [[DetailController alloc] initWithItem:theItem]; [self.navigationController pushViewController:nextController animated:YES]; [nextController release]; [positionInArray release]; [theItem release]; } </code></pre> <p>Now, everything works as expected. I select a row in the TableView which opens a DetailView with feed presented. Swiping left and right shows all feeds without problems. BUT! If I select the last feed or the one before last in the TableView, feed is presented as usual but then if I try to swipe left or right I get the following error:</p> <pre><code>2011-03-03 10:50:15.039 RssReader[72137:207] Swipe Right Movement done! 2011-03-03 10:50:15.041 RssReader[72137:207] *** -[CFNumber intValue]: message sent to deallocated instance 0x6240460 </code></pre> <p>I tried enabling zombies and Guard Malloc but this is the only thing that I got...</p> <p>The solution of the problem is what really makes me think do I even understand what memory allocation/deallocation means :p<br> If I remove the line <code>[positionInArray release]</code> OR <code>[theItem release]</code> app will work without any problems??? HOW? WHY? Oh, WHY?</p> <p>I can pretend that I understand that if I release the positionInArray object it cannot be passed around inside the other object NSMutableDictionarytheItem. I say pretend because I thought it's using a copy of it (by assigning).</p> <p>Also, what I can't understand is:</p> <ol> <li><p>why does it work for 13 feeds that I load and for the last two it doesn't?</p></li> <li><p>why does it work if I don`t release theItem object? I mean just few lines above that object was used to initialize the *nextController and there I made a local copy of it to work with it?</p></li> </ol> <p>If you need more information, please ask what could help you and I'll definitely provide it... Thanks in advance, L</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload