块中的retainCount显示外部行为 [英] retainCount in blocks show extrange behavior

查看:119
本文介绍了块中的retainCount显示外部行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个班级得到了这段代码:

I got the this code in a class:

- (void)cancel {
    if (_cancelBlock)
        _cancelBlock();
}
- (void)overrideCancelWithBlock:(void(^)(void))cancelBlock {
    [_cancelBlock release];
    NSLog(@"AsyncOperation-overrideCancelWithBlock-[cancelBlock retainCount]=%lu (before)", [cancelBlock retainCount]);
    _cancelBlock = [[cancelBlock copy] retain];
    NSLog(@"AsyncOperation-overrideCancelWithBlock-[_cancelBlock retainCount]=%lu (after)", [_cancelBlock retainCount]);
}

- (void)dealloc
{
    NSLog(@"AsyncOperation-dealloc-[_cancelBlock retainCount]=%lu (before)", [_cancelBlock retainCount]);
    [_cancelBlock release];
    NSLog(@"AsyncOperation-dealloc-[_cancelBlock retainCount]=%lu (after)", [_cancelBlock retainCount]);
    [super dealloc];
}

此NSLog()的输出为:

The output for this NSLog()'s are:

 AsyncOperation-overrideCancelWithBlock-[cancelBlock retainCount]=1 (before)
 AsyncOperation-overrideCancelWithBlock-[_cancelBlock retainCount]=1 (after)
 AsyncOperation-dealloc-[_cancelBlock retainCount]=1 (before)
 AsyncOperation-dealloc-[_cancelBlock retainCount]=1 (after) 

copy方法的文档说:

特殊注意事项

Special Considerations

如果您使用的是托管内存(不是 垃圾收集),这种方法 保留新对象之前 归还它.的调用者 方法,但是,负责 释放返回的对象.

If you are using managed memory (not garbage collection), this method retains the new object before returning it. The invoker of the method, however, is responsible for releasing the returned object.

所以.这样,NSLog()的输出对于retainCount总是显示相同的值?

So. Way the output of the NSLog() always show the same value for retainCount?

推荐答案

重要:此方法在调试内存管理问题时通常没有任何价值.由于可能有许多框架对象都保留了一个对象以保留对它的引用,而同时自动释放池可能在一个对象上保留了许多延迟的释放,因此,您不太可能从此对象中获得有用的信息.方法.

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

要了解必须遵守的内存管理基本规则,请阅读内存管理规则".要诊断内存管理问题,请使用合适的工具:

To understand the fundamental rules of memory management that you must abide by, read "Memory Management Rules". To diagnose memory management problems, use a suitable tool:

即使在运行程序之前,LLVM/Clang静态分析器通常也可以发现内存管理问题.
Instruments应用程序中的Object Alloc工具(请参见Instruments用户指南)可以跟踪对象的分配和销毁.
Shark(请参阅《 Shark用户指南》)还介绍了内存分配(在程序的许多其他方面).

The LLVM/Clang Static analyzer can typically find memory management problems even before you run your program.
The Object Alloc instrument in the Instruments application (see Instruments User Guide) can track object allocation and destruction.
Shark (see Shark User Guide) also profiles memory allocations (amongst numerous other aspects of your program).

回答您的问题:只有苹果可能知道为什么此时的keepCount是这样.

To answer your question: Only apple might know why the retainCount at this point is like it is.

这篇关于块中的retainCount显示外部行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆