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
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.
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.
in 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. and when putting a local
variable like NSArray in the block, the retain count fellows the same rule
as self.
there must be some thing different inside of @property, anyone researched
this before? 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.
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?
No comments:
Post a Comment