iddober
iddober

Reputation: 1264

Potential leak of an object allocated

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?

Upvotes: 8

Views: 8217

Answers (1)

warrenm
warrenm

Reputation: 31782

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.

Upvotes: 18

Related Questions