Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 782
iddober Avatar asked Jun 07 '10 21:06

iddober


1 Answers

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.

like image 155
warrenm Avatar answered Nov 15 '22 06:11

warrenm