Jim
Jim

Reputation: 5960

Objective-C Is there a way to tell if an object is set to autorelease?

I am looking at a leak report from instruments. Several of my leaks are pointing to the methods SBJSON. I have trouble believing the returned values are not autoreleased, but in tracing through the code, it becomes very tedious, including returning values using call-by-reference. I don't think code tracing is the right way to look at this.

Is there a way to tell if any object is retained within the autorelease pool? (I am not using ARC.)

UPDATE

Here is some code that Instruments is pointing me to:

- (NSString *) processResults:(NSString *) resultsGeoCodeString {

    NSDictionary *resultsGeoCode = [resultsGeoCodeString JSONValue]; // <--- 100%
    ...

I assume that the "100%" means that all of the leaks reported for this case originate in this line.

I don't release the object resultsGeoCode because I am assuming it is autoreleased.

The SBJSON kit I am using is not using ARC.

Upvotes: 2

Views: 142

Answers (3)

bbum
bbum

Reputation: 162722

Leaks is showing you the line of code that caused the allocation that is leaking, not the line of code that is causing the over-retain. Follow the resultsGeoCode dictionary around; assuming no bug in the library, there is an over-retain of that object somewhere.

You can use Instruments to show all retain/release events for any given object.

Upvotes: 2

JeremyP
JeremyP

Reputation: 86661

Generally speaking, knowing if an object is in the autorelease pool is not helpful. You may get an autoreleased object back from a method or you may get a constant object or a singleton or an object that the framework has retained internally for its own purposes.

Start with the assumption that it is your code and not SBJSON that is causing the leak. (SBJSON is a very popular and well used library and so probably doesn't have any serious leaks anymore.) Then examine what you do with the leaking object. For instance, do you put it in an array that is never deallocated for whatever reason. Do you autorelease it on a thread with no autorelease pool in effect and so on. Try posting you code here. Others may be able to spot the leak.

Upvotes: 2

Michael Chinen
Michael Chinen

Reputation: 18717

This is primitive, but you can override the autorelease, release and retain method (passing the message on to the super) and set a log message with a counter, (subclassing if necessary). Unfortunately I don't know another method as retainCount won't help you much here.

Upvotes: 1

Related Questions