-2

find a strange code here I have a viewcontroller, it has an array with books, and click the cell, then push to a detailViewController, detailVC has a variable infoDict,

 @property (nonatomic,retain) NSMutableDictionary * infoDict;

navigationController push

DetailViewController * tDVC = [[DetailViewController alloc] init];
tDVC.infoDict = infoDict;
[self.navigationController pushViewController:tDVC animated:YES];
[tDVC release];

tap the back button, pop back, and inside dealloc of DetailVC

-(void)dealloc
{
    [super dealloc];

    NSLog(@"before %d",infoDict.retainCount);
    [infoDict release];
}

but when I tap back, in this dealloc app crashes randomly, EXC_BAD_ACCESS.

when move [super dealloc] to the bottom of dealloc, it seems back to normal. pls help me to understand this , many thanks

B.S.
  • 21,170
  • 14
  • 82
  • 108
nickyu
  • 141
  • 1
  • 11

3 Answers3

4

[super dealloc] deallocates the object itself. If you type dealloc and let Xcode autocomplete, you get this:

- (void)dealloc
{
    <#deallocations#>
    [super dealloc];
}

Meaning you should release objects and properties before you call [super dealloc]

Bob Vork
  • 2,879
  • 23
  • 31
2

Your -dealloc implementation is out of order. The call to -[super dealloc] must be the absolute last invocation in the dealloc method. When you access the ivar infoDict, the compiler is really doing something like self->infoDict and by this point, self has been deallocated and is no longer valid.

If at all possible, I recommend using ARC instead of manually managing memory.

Jason Coco
  • 76,627
  • 20
  • 180
  • 178
-1

Try

its better if you use self.infoDict = nil; instead of [infoDict rlease];

-(void)dealloc
{
  NSLog(@"before %d",infoDict.retainCount);
  [infoDict release];

  [super dealloc];
}
Jason Coco
  • 76,627
  • 20
  • 180
  • 178
iOS Coder
  • 39
  • 1
  • 2
  • 4
  • 1
    It's definitely not best to use accessors in `-dealloc`. It's best to use ARC and have no need for `-dealloc` anymore, but if you can't, you should not use accessors in `-dealloc` or `-init` – Jason Coco Aug 29 '13 at 07:39
  • best is Arc but if not using Arc and as the guy said he is using dealloc so it mean he is not using Arc – iOS Coder Aug 29 '13 at 16:16
  • Yep, I got that, my comment was in response to your suggestion that it's better to use the accessors (e.g., `self.infoDict = nil;` in your `-dealloc`. You should not use accessors in deallocation or initialization routines. – Jason Coco Aug 29 '13 at 17:24
  • @JasonCoco When I dug into this, for iOS 3.0, Apple's own best practice was setting them to nil, because it correctly handles weird edge-cases around KVO/listeners/etc (IIRC), whereas merely "releasing" can give you some nasty crashes. Do you have a link to prove that there is anything wrong with setting to nil? – Adam Jan 26 '14 at 22:02
  • @Adam IMHO it's common sense and OO best practices not to access self while destroying or creating an object. If you have side effects in your accessors, you should execute them—if necessary—in your destructor. Apple have said it during talks and in various papers, but spell it out specifically here: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW6 – Jason Coco Jan 27 '14 at 22:28
  • You can also see in the 3.1 release notes (link below) that Apple even changed their behavior to directly release the view and no longer call accessor methods on `UIViewController`: https://developer.apple.com/library/ios/releasenotes/General/RN-iPhoneSDK-3/ – Jason Coco Jan 27 '14 at 22:32
  • UIViewController is a special case - the .view property already breaks Apple's own rules (and always will). – Adam Jan 31 '14 at 13:02