Re: Weird crash on iOS
Re: Weird crash on iOS
- Subject: Re: Weird crash on iOS
- From: "Glenn L. Austin" <email@hidden>
- Date: Wed, 19 Sep 2012 00:36:46 -0700
On Sep 18, 2012, at 10:50 PM, Laurent Daudelin <email@hidden> wrote:
> On Sep 18, 2012, at 22:35, Glenn L. Austin <email@hidden> wrote:
>
>> Just one of the (multiple) reasons I despise the "afterDelay" version.
>>
>> Almost every crash I see is directly (or indirectly) caused by using performSelector:withObject:afterDelay: to insert some kind of "pause" in the application, and that pause isn't cancelable (AFAIK) -- the selector WILL be called after the delay time, even if the user has moved to another part of the program.
>
> Glenn,
>
> Can you elaborate on the last part?
Sure. I often see people use performSelector:withObject:afterDelay: as a way to implement a "state machine" where they move from method to method using performSelector:withObject:afterDelay: as a way to "not block" the main thread.
Now, imagine that the user leaves that view to go back to the main view and in order to do this you popped the navigator stack (or otherwise modified it), that view is gone when the selector is performed, and BOOM!
A far better way is to either use a pre-defined queue of operations, or now use GCD. Whatever you do should be easily cancelable. If you're using a queue, cancel the queue contents -- otherwise set a flag that your code can check before moving to the next state.
These kinds of crashes are hard to diagnose and track down, but in every case of non-deterministic crashes I've seen have been related to using some kind of timer. For pre-4.0, I switched code out to either use a queue and performSelectorOnMainThread:withObject:, or a thread that slept for the designated time and checked the "cancelled" flag before calling the selector. Yes, it does mean keeping track of an additional object, but rock-solid stability (one product I worked on went from something like 100 distinct reported crash types to zero) was worth the little bit of extra care.
--
Glenn L. Austin, Computer Wizard and Race Car Driver <><
<http://www.austin-soft.com>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden