ios5 - Copied blocks and CLANG leak warnings -
i have method takes block:
- (void)methodwithblock:(blocktype)block
the method starts out copying block
, because asynchronous things before using it, , discarded otherwise. calls method within block, , releases it, within block. summarily:
- (void)methodwithblock:(blocktype)block { block = [block copy]; [something asyncstuffwithfinishedblock:^{ // .. block(); [block release]; }]; }
clang complains memory leaks "block". if remove copy , release statements block gone time it's called -- @ least earlier crashes indicates case.
is wrong way things? if so, how should above -- i.e. block callback within block statement in method? can't store block instance variable, method called repeatedly different arguments while asynchronous part happening.
first, -copy
, -release
should unnecessary. -asyncstuffwithfinishedblock:
method must copy block that's passed it. when block copied , references other block objects, copies block objects, too. need figure out real nature of crash seeing.
second, releasing wrong thing. [block copy]
not modify block
(the receiver of -copy
message) somehow turning copy. returns copy of block. returned copy want reference both in invocation statement (block();
) , when releasing.
so, do:
block = [block copy]; [something asyncstuffwithfinishedblock:^{ // .. block(); [block release]; }];
note reassignment of local block
variable point copy.
think of way: ever copy string way attempted copy block? do:
[somestring copy]; // ... use somestring ... [somestring release];
Comments
Post a Comment