Apple doc says "In a manually reference-counted environment, local variables used within the block are retained when the block is copied. Use of instance variables within the block will cause the object itself to be retained. "
Apple doc说:“在手动引用计数的环境中,当块被复制时,块中使用的局部变量将被保留。在块中使用实例变量将导致保留对象本身。”
I was going to check local variable is retained by block or not using retainCount, but failed. Can anyone help me?
我打算检查局部变量是否被块保留或不使用retainCount,但是失败了。谁能帮我?
2 个解决方案
#1
1
A block won't retain an object unless the block is copied. Since a block can only capture state within the same scope as that captured state, the implementation assumes no need to actually retain anything unless the block is copied for the purposes of escaping the scope of declaration.
除非复制块,否则块不会保留对象。由于块只能捕获与捕获状态相同范围内的状态,因此实现假定不需要实际保留任何内容,除非为了转义声明范围而复制块。
Think of it in terms of "execution pointer" (kinda like when you are stepping through code in the debugger).
可以用“执行指针”来思考它(有点像在调试器中单步执行代码时)。
When the execution pointer passes over a block's declaration, that block captures a snapshot -- copies -- all variables that are used within the block's scope that are not declared within the block itself. For an object, that means the block makes a copy of the reference to the object, not a copy of the object itself.
当执行指针经过块的声明时,该块捕获快照 - 复制 - 在块的范围内使用的所有变量,这些变量未在块本身内声明。对于一个对象,这意味着该块会复制对象的引用,而不是对象本身的副本。
A block starts on the stack. When a block is copied the first time, it is copied from the stack to the heap using a compiler generated per-block "copy helper" (a simple block may not have a copy helper and might actually never be on the stack). That copy helper will retain any objects referenced by the block (that are not referenced via an __block
variable anyway).
一个块从堆栈开始。当第一次复制块时,使用编译器生成的每块“复制助手”将其从堆栈复制到堆(一个简单的块可能没有复制助手,实际上可能永远不会在堆栈中)。该复制助手将保留块引用的任何对象(无论如何都不通过__block变量引用)。
They won't be released until the block is released and deallocated.
在块被释放和解除分配之前,它们不会被释放。
#2
0
retainCount
is useless.
retainCount没用。
trust the documentation for the implementation. if that fails or defies your expectation for some reason, provide a sample program.
信任实施的文档。如果由于某种原因失败或违背您的期望,请提供示例程序。
there's no need to verify the retain count, because that's how it works.
没有必要验证保留计数,因为它是如何工作的。
if you do doubt it and want to sanity check it, Instruments can be configured to record reference count operations of NSObject
s. in that case, run as usual (launched from Instruments, of course), locate the instances of interest in the list of allocated objects, then evaluate the backtraces of the ref-count-ops of the objects of interest. you should see it in there.
如果你确实怀疑它并想要理智地检查它,可以配置Instruments来记录NSObjects的引用计数操作。在这种情况下,像往常一样运行(当然是从Instruments启动),在分配的对象列表中找到感兴趣的实例,然后评估感兴趣对象的ref-count-ops的回溯。你应该在那里看到它。
#1
1
A block won't retain an object unless the block is copied. Since a block can only capture state within the same scope as that captured state, the implementation assumes no need to actually retain anything unless the block is copied for the purposes of escaping the scope of declaration.
除非复制块,否则块不会保留对象。由于块只能捕获与捕获状态相同范围内的状态,因此实现假定不需要实际保留任何内容,除非为了转义声明范围而复制块。
Think of it in terms of "execution pointer" (kinda like when you are stepping through code in the debugger).
可以用“执行指针”来思考它(有点像在调试器中单步执行代码时)。
When the execution pointer passes over a block's declaration, that block captures a snapshot -- copies -- all variables that are used within the block's scope that are not declared within the block itself. For an object, that means the block makes a copy of the reference to the object, not a copy of the object itself.
当执行指针经过块的声明时,该块捕获快照 - 复制 - 在块的范围内使用的所有变量,这些变量未在块本身内声明。对于一个对象,这意味着该块会复制对象的引用,而不是对象本身的副本。
A block starts on the stack. When a block is copied the first time, it is copied from the stack to the heap using a compiler generated per-block "copy helper" (a simple block may not have a copy helper and might actually never be on the stack). That copy helper will retain any objects referenced by the block (that are not referenced via an __block
variable anyway).
一个块从堆栈开始。当第一次复制块时,使用编译器生成的每块“复制助手”将其从堆栈复制到堆(一个简单的块可能没有复制助手,实际上可能永远不会在堆栈中)。该复制助手将保留块引用的任何对象(无论如何都不通过__block变量引用)。
They won't be released until the block is released and deallocated.
在块被释放和解除分配之前,它们不会被释放。
#2
0
retainCount
is useless.
retainCount没用。
trust the documentation for the implementation. if that fails or defies your expectation for some reason, provide a sample program.
信任实施的文档。如果由于某种原因失败或违背您的期望,请提供示例程序。
there's no need to verify the retain count, because that's how it works.
没有必要验证保留计数,因为它是如何工作的。
if you do doubt it and want to sanity check it, Instruments can be configured to record reference count operations of NSObject
s. in that case, run as usual (launched from Instruments, of course), locate the instances of interest in the list of allocated objects, then evaluate the backtraces of the ref-count-ops of the objects of interest. you should see it in there.
如果你确实怀疑它并想要理智地检查它,可以配置Instruments来记录NSObjects的引用计数操作。在这种情况下,像往常一样运行(当然是从Instruments启动),在分配的对象列表中找到感兴趣的实例,然后评估感兴趣对象的ref-count-ops的回溯。你应该在那里看到它。