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]开发者_如何学C 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.
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.
精彩评论