ios - Why my code crash only in release and after upgrade to Xcode 4.6? -
i got crash due deallocating of variable holds reference block beingness executed. here code example:
this wrong in release, in debug on same device runs ok, must run add-hoc crash.
- (void)test { _test = [self dolater:^{ _count++; [self test]; } :3]; }
this defined in nsobject category:
- (dolaterprocess *)dolater:(void (^)())method :(double)delay { homecoming [[dolaterprocess new] from:method :delay]; }
end implementation of used class:
@implementation dolaterprocess { id _method; bool _stop; } - (void)methodtoperform:(void (^)())methodtoinvoke { if (_stop)return; if (nsthread.ismainthread) methodtoinvoke(); else [self performselectoronmainthread:@selector(methodtoperform:) withobject:methodtoinvoke waituntildone:no]; } - (dolaterprocess *)from:(void (^)())method:(nstimeinterval)delay { [self performselector:@selector(methodtoperform:) withobject:method afterdelay:delay]; _method = method; homecoming self; } - (void)stop { _stop = yes; [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(methodtoperform:) object:_method]; } @end
so understand _test variable deallocated , block while in deallocated? why crashes? why doesn't crash in debug, can forcefulness somehow compiler crash on in debug? give thanks you.
blocks capture local state. in case block capturing _count
, self
. in order efficiently, when create block lives on stack, effect safe used long method doesn't return. can pass blocks downward can't maintain them or pass them upward without moving them heap. accomplish copy
ing them (and copy
defined deed retain
if block on heap, don't pay over-copying).
in case, right thing dolater::
re-create block (though, record, unnamed parameters considered extremely poor practice).
i'm bit confused why both assign method instance variable , schedule later pass forward, quickest prepare be:
- (dolaterprocess *)from:(void (^)())method:(nstimeinterval)delay { method = [method copy]; [self performselector:@selector(methodtoperform:) withobject:method afterdelay:delay]; _method = method; homecoming self; }
as why appears have become broken under 4.6: relying on undocumented behaviour (albeit undocumented behaviour feels should natural) alter in toolset or os (or indeed, no alter whatsoever) permitted impact that.
(aside: seem reimplementing lot of built gcd; straight replace from::
dispatch_after
, methodtoperform:
dispatch_async
, in both cases supplying dispatch_get_main_queue()
queue).
ios objective-c xcode4.6
No comments:
Post a Comment