objective c - IOS block not retain self? -


im new block programming in ios, ive read many guides , say, things retained in block, , write demo test retain cycle mentioned.

header file:

typedef nsstring* (^myblock)(void);  @interface detailviewcontroller : uiviewcontroller <uisplitviewcontrollerdelegate> {      uiview * testview;      subdetailviewcontroller * tsubdetailviewcontroller;      nsmutablearray * array;      myblock block1;  } 

m file: 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 array , self retained, retatincount +1; whether self.block1(), or not, retaincount not +1, seems fine, array released,when pop view controller, self release normally.

do miss guides? curious abt situation. give me code of retain cycle block?

blocks retain captured variables when block copied. since using mrc, compiler not automatically. assigning block literal directly instance variable block1 (not through property or something), no copying done.

this incorrect use of blocks, because time store block (e.g. in instance variable) in place out-live current scope, must copy it. block literals valid in scope defined in. because blocks may start out on stack, , must copied moved heap; otherwise, destroyed @ end of scope. if try use block pointed block1 after function done, bad things happen.

so if had used blocks correctly, have copied block, block retain self. in addition, instance variable, block should retained self, in order follow memory management rules. have retain cycle.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -