Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString initWithFormat memory leak stumper

XCode is suggesting that an [NSString initWithFormat] method (as listed in the Instruments window) is the source of a memory leak. It lists the particular method, so in that method I identified the only line of code with initWithFormat (as unlike some leaks identified in Instruments, this one will not take me to the exact offending line of code for some reason):

 NSString * name=[[NSString alloc] initWithFormat:@"%@",[[lineArray objectAtIndex:0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ];

 CustomObject * newData=[[CustomObject alloc] init];
 newData.info=name; // info is a retained NSString ivar of CustomObject

 [name release];
 [self.ivar setObject:newData forKey:@"key"];
 [newData release];

The initWithFormat and the release are within the same scope: both occur inside an if statement.

Are there some NSString methods that somehow call initWithFormat without it being obvious in the code? Such as these stringByTrimmingcharactersInSet type methods?

UPDATE: I've added all the code between the initWithFormat and the release -- Also, even if I change initWithFormat to stringWithFormat, Xcode gives me the same memory leak, instead identifying stringWithFormat, so this is clearly the object causing the problem.

like image 542
johnbakers Avatar asked Jun 05 '26 05:06

johnbakers


1 Answers

Instruments doesn't show where the object was leaked; it shows where the object that was leaked was created. So what's probably going on is you're not releasing the info ivar of your CustomObject class in its dealloc method. Either that, or you're over-retaining that object somewhere else.

like image 159
Dave DeLong Avatar answered Jun 06 '26 20:06

Dave DeLong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!