简体   繁体   English

iOS块不能保留自我吗?

[英]IOS block not retain self?

Im new to the block programming in ios, Ive read many guides and they say, things get retained in a block, and I write a demo to test the retain cycle they mentioned. 我是ios的块编程的新手,我读了许多指南,他们说,东西被保留在块中,我编写了一个演示来测试他们提到的保留周期。

header file: 头文件:

typedef NSString* (^MyBlock)(void);

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
{

    UIView * testView;

    SubDetailViewController * tSubDetailViewController;

    NSMutableArray * array;

    MyBlock block1;

}

m file: in viewDidLoad : m文件:in viewDidLoad

array = [[NSMutableArray alloc] init];

block1 = ^(void){

    [array addObject:@"23"];

    [self btn2Touch:nil];

    return @"3";
};
NSLog(@"self after block retainCount -> %d",self.retainCount);
NSLog(@"array after block  retainCount -> %d",array.retainCount);

//self.block1();

[array release];

I thought the array and self will be retained, retatinCount +1; 我以为数组和自我将被保留,retatinCount +1; but whether I do self.block1(), or not, the retainCount not +1, everything seems fine, array could be released,when pop the view controller, self release normally. 但是无论我是否执行self.block1(),remainCount不是+1,一切似乎都很好,可以释放数组,当弹出视图控制器时,self可以正常释放。

do I miss something with guides? 我会错过一些向导吗? so curious abt this situation. 很奇怪这种情况。 anyone could give me a code of retain cycle with block? 任何人都可以给我一个带有块的保留周期代码?

Blocks retain their captured variables when the block is copied. 复制块时,块保留其捕获的变量。 Since you are using MRC, the compiler does not do anything automatically. 由于您使用的是MRC,因此编译器不会自动执行任何操作。 You are assigning the block literal directly to the instance variable block1 (not through a property or something), so no copying is done. 您是直接将块文字分配给实例变量block1 (而不是通过属性或其他方式),因此不会进行任何复制。

This is incorrect use of blocks, because any time where you store a block (eg in an instance variable) in a place that will out-live the current scope, you must copy it. 这是对块的不正确使用,因为任何时候将块(例如,在实例变量中)存储在将超出当前范围的位置,则必须将其复制。 Block literals are only valid in the scope they are defined in. This is because blocks may start out on the stack, and must be copied to be moved into the heap; 块字面量仅在定义它们的范围内有效。这是因为块可能始于堆栈,必须将其复制后才能移入堆中。 otherwise, they are destroyed at the end of the scope. 否则,它们将在作用域末尾销毁。 So if you try to use the block pointed to by block1 after this function is done, bad things will probably happen. 因此,如果在此功能完成后尝试使用block1指向的块,则可能会发生不良情况。

So if you had used blocks correctly, you would have copied the block, thus the block would retain self . 因此,如果您正确使用了块,那么您将复制该块,因此该块将保留self In addition, as an instance variable, the block should also be retained by self , in order to follow memory management rules. 此外,作为实例变量,该块还应由self保留,以便遵循内存管理规则。 So you have a retain cycle. 因此,您有一个保留周期。

This block is in stack, a block in stack will not retain object in its body. 此块位于堆栈中,堆栈中的块不会在其主体中保留对象。 Instead, a block in heap, will retain object in its body. 相反,堆中的一个块会将对象保留在其主体中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM