简体   繁体   English

iOS区块释放和自我保留周期

[英]iOS block release and self retain cycle

Im doing some research on blocks, the code here 我正在对块进行一些研究,这里的代码

typedef NSString* (^MyBlock)(void);
@property(copy,nonatomic) MyBlock block1;

in viewdidload 在viewdidload

self.block1 = ^{
    self
    NSLog(@"do block");
    return @"a";
};

of course the self is retained, then I do a 当然,自我被保留,然后我做一个

self.block = nil;

by checking the retain count of self, I found it reduced by 1, no retain cycle. 通过检查自己的保留计数,我发现它减少了1,没有保留周期。

I believe this is the correct situation, the block retains self, when release the block, self gets released. 我相信这是正确的情况,该块保留了自身,释放块时,其自身被释放了。 retaincount reduced. 保留数减少。

I made a litte change, and things coms strange: make block a local variable. 我做了一个小小的改变,事情变得奇怪了:make block是一个局部变量。

in viewdidload 在viewdidload

MyBlock block1 = ^{
    self
    NSLog(@"do block");
    return @"a";
};

[block copy]; // retain count of self gets added.
[block release];  // retain count of sell still the same

why? 为什么? I tried Block_release(), its the same. 我尝试了Block_release(),同样。 and when putting a local variable like NSArray in the block, the retain count fellows the same rule as self. 当将类似NSArray的局部变量放入块中时,保留计数与self的规则相同。

there must be some thing different inside of @property, anyone researched this before? @property内部一定有一些不同的东西,以前有人研究过吗? pls help. 请帮助。

Additionally, I do this in ARC, a local variable block will made the retain cycle, and a instance variable didnt, due to the autorelease, it holds the self, and few seconds later, it released and self object get released normally. 另外,我在ARC中执行此操作,局部变量块将导致保留周期,而实例变量则没有,由于自动释放,它保留了self,几秒钟后,它释放了并且self对象正常释放了。

is it because the instance variable and local variable are alloc on different parts in memory? 是因为实例变量和局部变量分配在内存中的不同部分吗? stack ? 堆栈? heap?, are they both copied to heap when do a [block copy]? 堆?,它们在执行[块复制]时是否都复制到堆上?

EDIT : not instance variable and local variable. 编辑:不是实例变量和局部变量。 using @property makes it different, any explanations? 使用@property会有所不同,有什么解释吗?

The problem is that using retainCount to figure out things like this is futile. 问题在于,使用retainCount来找出类似的事情是徒劳的。 The retainCount will not reflect autorelease state and the compiler -- the ARC compiler, in particular -- may generate different code (in particular, the optimizer has a tendency to eliminate unnecessary retain/autorelease pairs). retainCount将不会反映autorelease状态,并且编译器(尤其是ARC编译器)可能会生成不同的代码(特别是,优化程序倾向于消除不必要的保留/自动释放对)。

Use the Allocations Instrument and turn on reference event tracking. 使用分配工具并打开参考事件跟踪。 Then you can look at each retain/release event on the object, the exact stack trace where it happened, and know exactly what is going on. 然后,你可以看看每个对象,确切的堆栈跟踪它发生在保留/释放事件,并知道到底是怎么回事。

Under non-ARC, when you assign to an iVar, nothing happens. 在非ARC下,当您分配给iVar时,什么也不会发生。 When you go through the setter, a retain setter will cause the object to be retain d. 当您通过设置器时, retain设置器将使对象被retain d。 Under ARC, a block will be copied to the heap automatically in a number of cases, triggering a retain of the captured object when the block is copied. 在ARC下,在许多情况下,块将自动复制到堆中,并在复制块时触发捕获对象的保留。

http://www.whentouseretaincount.com/ http://www.whentouseretaincount.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS块不能保留自我吗? - IOS block not retain self? 如果在块内引用self,为什么这不是保留周期? - Why is this not a retain cycle if self is referred to within a block? iOS 5 Twitter Framework和completionHandler块 - “在这个块中强烈捕获'自我'可能会导致保留周期” - iOS 5 Twitter Framework & completionHandler block - “Capturing 'self' strongly in this block is likely to lead to a retain cycle” 什么时候在块中引用自身? - When is reference to self in block a retain cycle? 块中的@synchronized(self)是否会导致保留周期? - Does @synchronized(self) in a block lead to a retain cycle? 如果一个强属性具有一个涉及自我的障碍,那是一个保留周期吗? - If a strong property has a block that refers to self, is that a retain cycle? 在这个区块中强烈捕获自我可能会导致保留周期 - capturing self strongly in this block is likely to lead to a retain cycle 避免“在此块中强烈捕获自我可能会导致保留周期”消息 - Avoiding the “capturing self strongly in this block is likely to lead to a retain cycle” message “使用可达性”“在此区块中强烈捕获'自我'很可能导致保留周期” - “Capturing 'self' strongly in this block is likely to lead to a retain cycle” using Reachability 使用块保留`self`循环 - Retain cycle on `self` with blocks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM