objective c - Can I release an object created outside a block that uses it before the block is run? -
can myarray safely released before block?
nsmutablearray *myarray = [[nsmutablearray alloc] init]; [myarray addobject:@"a"]; [myarray addobject:@"b"]; // releasing here causes invalid object used inside block? [myarray release]; cccallblock *block = [cccallblock actionwithblock:^{ // print myarray contents console }]; [mynode runaction:block];
no, cannot release array before block, because objects inside array , array not usable after call of release, before block gets chance retain it.
you can make array autoreleased, in case release happen after function exits.
nsmutablearray *myarray = [nsmutablearray array]; [myarray addobject:@"a"]; [myarray addobject:@"b"]; cccallblock *block = [cccallblock actionwithblock:^{ // print myarray contents console }]; [mynode runaction:block];
Comments
Post a Comment