views:

44

answers:

2

I create/push my ViewController, display some informations and navigate back to my RootViewController. While I navigate back the app crashes (EXC_BAD_ACCESS).

With Instruments I found out that the dealloc of my ViewController is called, which releases my wrapper class. In my wrapper class also the dealloc method is called. This dealloc releases parsedJson (retained property) and right then the app crashes. It is strange that the retain count drops from 1 to -1 despite only a malloc and a release has been made.

If I retain the returned result parsedJson, the app doesn't crash and I don't find a leak.

SBJsonParser *JSONParser = [SBJsonParser new];
id parsedJson = [[JSONParser objectWithString:jsonString error:NULL] retain];

I can't make a release/autorelease because then the app would crash again.

With NSZombieEnabled I get

* -[CFDictionary release]: message sent to deallocated instance 0x6d51aa0

I get this Zombie after I made a change. Before I used a property and Interface Builder for my view controller and I didn't got a crash, because the view controller wasn't released immediately. Now I'm doing this:

MyViewController *myViewController = [[MyViewController alloc] init];
[[self navigationController] pushViewController:myViewController animated:YES];
[myViewController release];
myViewController = nil;

Where is the error? I cannot retain parsedJSON without a release. I'm never releasing parsedJson except of the dealloc. Has the used library an error in it?

A: 

After I returned the value to my other method I zero parsedJSON

parsedJSON = nil;

So if parsedJSON get dealloced nothing happens, because a message send to nil doesn't cause the app to crash. Despite I didn't found the cause of the over-release ...

testing
+1  A: 

[JSONParser objectWithString:] returns an auto-released object. If you want to keep it around, you must retain it - and only at that point do you become responsible for releasing it. But if you didn't retain it, then don't keep it around and don't release it.

You said you were using a retain property, but did you actually use the property (self.ivar = object) or did you access the variable directly (ivar = object)? The object will only be retained if you use the property.

Pieter Witvoet
I set the property like ivar=object. Didn't realized that fact, that I have to use the setter method (self.ivar or [self setIvar:myVariable]). Thanks a million for that!
testing