开发者

NSString initWithFormat memory leak stumper

开发者 https://www.devze.com 2023-04-01 01:34 出处:网络
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 on

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号