开发者

SBJsonParser: App crashes if I do not retain the return value

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 i开发者_StackOverflow中文版s 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?


[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.


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 ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜