views:

131

answers:

1

Using the build and analyze of XCode I saw i have a memory leak in my code:

- (NSString *) doIt
{
    NSString *var = [[NSString alloc] init];

    return var;
}

This is of course a simplified snippet of my problem

where do i release the object?

+4  A: 

This is a perfect situation for autorelease.

return [var autorelease]; will return the object with its present retain count of 1 and decrement the retain count of the object at some point in the future, after which the calling code should have retained the object if it needs to.

warrenm
It's deterministic — the object will be released when the current autorelease pool is drained. It's just not usually important since the object is no longer your responsibility and it's guaranteed not to be for the duration of your method as long as you're not creating a pool yourself.
Chuck
That was poor word choice. I assumed that the OP's object would be going into the default autorelease pool and as such, he would have no control over when it was released. Corrected above.
warrenm