Instruments 说我泄漏了一根弦,但我找不到它
我使用Instruments中的leaks工具来测试代码,但是leaks工具似乎无法找到泄漏。
在我的代码末尾, NSLog(@"str count:%d",[str keepCount]);
的输出是 3。为什么?我不会覆盖dealloc。 [a.name keepCount] 是否只有一次 我只自动释放 str 一次。所以 str 不应该泄漏。
@interface DataMode : NSObject {
NSString * name;
}
@property (retain) NSString * name;
- initWithName:(NSString * )name_;
@end
@implementation DataMode
@synthesize name;
- initWithName:(NSString * )name_
{
if ([super init] != nil)
{
name = name_;
return self;
}
return nil;
}
@end
- (void) pressed:(id)sender
{
for( int i = 0;i<10000000;i++)
{
NSString * str = [NSString stringWithFormat:@"zhang"];
DataMode * a = [[DataMode alloc] initWithName:str];
NSLog(@"a0 count:%d",[a retainCount]);
NSLog(@"name1 count:%d",[a.name retainCount]);
NSLog(@"name1 count:%d",[a.name retainCount]);
NSLog(@"a1 count:%d",[a retainCount]);
[ a release];
NSLog(@"str count:%d",[str retainCount]);
NSLog(@"str count:%d",[str retainCount]);
}
}
@end
I used the leaks tool in Instruments to test the code, but the leaks tool cannot seem to find the leak.
At the end of my code, the output of NSLog(@"str count:%d",[str retainCount]);
is 3. Why? I don't override the dealloc. [a.name retainCount] is there just one time
and I only autorelease str for one time. So str shouldn't leak.
@interface DataMode : NSObject {
NSString * name;
}
@property (retain) NSString * name;
- initWithName:(NSString * )name_;
@end
@implementation DataMode
@synthesize name;
- initWithName:(NSString * )name_
{
if ([super init] != nil)
{
name = name_;
return self;
}
return nil;
}
@end
- (void) pressed:(id)sender
{
for( int i = 0;i<10000000;i++)
{
NSString * str = [NSString stringWithFormat:@"zhang"];
DataMode * a = [[DataMode alloc] initWithName:str];
NSLog(@"a0 count:%d",[a retainCount]);
NSLog(@"name1 count:%d",[a.name retainCount]);
NSLog(@"name1 count:%d",[a.name retainCount]);
NSLog(@"a1 count:%d",[a retainCount]);
[ a release];
NSLog(@"str count:%d",[str retainCount]);
NSLog(@"str count:%d",[str retainCount]);
}
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保留计数没有用。别叫它。
它对于查找泄漏没有什么用处,因为有更好、更准确、误导性更少的工具可用。
您的代码存在几个问题(但泄漏不是其中之一):
NSString* 属性应该是
copy
您不使用该属性在
init
中设置字符串值,因此 DataMode 实例不会保留其字符串。没有 dealloc 方法
至于保留计数;我很惊讶它是“3”。我希望它是 2bazillionsomething,因为这是一个常量字符串(常量字符串的 stringWithString: 只是返回字符串)。因为您使用了stringWithFormat:
,常量字符串转为非常量字符串。如果您使用了常量字符串或stringWithString:
,那么它将是无数的东西(无符号-1...UINT_MAX...)。无论如何,您有:
stringWithString:
a.name
a.name
+3 总体。
如果 Instruments 声称存在泄漏,请发布屏幕截图。
retainCount is useless. Don't call it.
It is not useful for finding leaks as there are much better, more accurate, and less misleading tools available.
There are several problems with your code (but leaking isn't one of them):
NSString* properties should be
copy
you don't use the property to set the string value in
init
, thus the DataMode instances are not retaining their strings.there is no dealloc method
As for the retain counts; I'm surprised it is "3". I'd expect it to be 2bazillionsomething as that is a constant string (andSince you usedstringWithString:
of a constant string just returns the string).stringWithFormat:
, the constant string is turned into a non-constant string. If you had used the constant string orstringWithString:
, it'd be abazillionsomething (unsigned -1... UINT_MAX...).In any case, you have:
stringWithString:
a.name
a.name
+3 overall.
If Instruments is claiming a leak, post a screenshot.
我引用 NSObject 协议参考 对于
-retainCount
:由于多种原因,保留计数可能为 3;如果您无法使用泄漏工具找到泄漏,则很可能没有泄漏。不必担心保留计数的实际值。
如果您真的对为什么是 3 感兴趣,请记住:
a
的引用可能会一直保留,直到最近的自动释放池耗尽为止str
变量中保存一个引用I quote the NSObject protocol reference for
-retainCount
:The retain count could be 3 for any number of reasons; if you can't find a leak with the leaks tool, it's likely you don't have a leak. Don't worry about the actual value of the retain count.
If you're really interested in why it's 3, recall that:
a
will likely be held until the closest autorelease pool is drainedstr
variable由于您使用便捷方法来创建自动释放的
str
,因此您将不会看到以这种方式使用保留计数的确定行为。检查我对另一个问题的回答,并将这些方法添加到 DataMode 中,您应该会看到框架何时从自动释放池中释放您的对象。
在类中覆盖release和retain
Since you are using the convenience method to create
str
which is autoreleased you will not see determinate behavior using retain counts in this way.Check my response to another question and add those methods to
DataMode
and you should see when the framework releases your objects from the autorelease pool.Overriding release and retain in your class