NSString 内存泄漏
- (NSString*) getProjectCoreName
{
return [NSString stringWithFormat:@"%@_%ld", kTLProject, sProjectCores++];
}
Instruments 告诉我上述函数泄漏了 32 个字节。该字符串用作静态 NSMutableDictionary 中的键:
[dictionary setObject:instance forKey:name];
该字典在程序运行过程中永远不会释放。这是泄漏吗?这是一个 MacOS 应用程序。
字典是静态定义的:
static NSMutableDictionary *dictionary = nil;
然后:
if(dictionary == nil){
dictionary = [NSMutableDictionary dictionaryWithCapacity:5];
[dictionary retain];
};
- (NSString*) getProjectCoreName
{
return [NSString stringWithFormat:@"%@_%ld", kTLProject, sProjectCores++];
}
Instruments is telling me 32 bytes is leaking from the above function. The string is used as a key in a static NSMutableDictionary:
[dictionary setObject:instance forKey:name];
This dictionary is never released during the course of the program. Is this a leak? This is a MacOS application.
The dictionary is defined statically:
static NSMutableDictionary *dictionary = nil;
Then later:
if(dictionary == nil){
dictionary = [NSMutableDictionary dictionaryWithCapacity:5];
[dictionary retain];
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该函数本身不包含内存泄漏。
stringWithFormat
返回一个自动释放的对象,你也是。如果有泄漏,则一定是在其他地方。This function itself does not contain a memory leak.
stringWithFormat
returns an autoreleased object and so are you. If there is a leak it must be somewhere else.