benwad
benwad

Reputation: 6594

Strange memory leak when calling SBJSON

I've been thoroughly testing my app using the Leaks tool in Instruments, and occasionally a leak comes up when using SBJSON. Having looked over the net it appears that SBJSON tends not to leak by itself, so it must be the way I am calling it. Here's a screenshot of the offending line in my code, pointed to by the Leaks instrument:

enter image description here

This leak is detected about 15 minutes into execution in this particular run, and is completely unpredictable in when it may happen. Instruments says the leaked memory is of type NSNumber, and this highlighted line is contained in a method that is called all the time throughout the execution of the app. I've tried outputting the value of the _source string to the console but there's nothing strange about the output when the leak occurs. Here's another screenshot showing the history of the leaked block:

enter image description here

I am running the app on the iPhone 4.2 simulator, and my testing basically involves clicking around every view in the application to make sure it all runs okay. As you may be able to see in the screenshot above, the JSONValue call goes to the method defined in NSString+SBJSON.m so I'm pretty sure if there is a problem it's in my code. Any idea what I am doing wrong?

Upvotes: 0

Views: 382

Answers (1)

Nekto
Nekto

Reputation: 17877

When you assign _object = [[_source JSONValue] retain]; you increase reference count of object returned by JSONValue. In method initWithData:(NSData *)data this object is not released. So Analyzer thinks that there is memory leak.

You should check if you are releasing _object before losing reference to it or in dealloc method:

[_object release];

Upvotes: 1

Related Questions