iphone - Objective-C multithreading, what happens when an object gets dealloc while its methods are being executed? (And how to prevent it?) -


i writing app iphone , arrived @ situation.

i have view controller, myviewcontroller, dealloc whenever user taps "back" button on screen. there thread in background communicates remote server , may message method, updateui, method in myviewcontroller.

what happen if background thread messages updateui in myviewcontroller, user happened tapped "back" button @ right time such causes myviewcontroller dealloc while updateui still executing?

my guess dealloc method run , app might crash if updateui ends using null pointer. assuming case, current solution have is:

[self retain]; // updateui code here [self release]; 

i unsure if best solution, feel common problem when dealing multiple threads.

is assumption correct? if so, there better solution?

what describing known "race condition." race conditions can difficult identify in testing, track down once reported, , reproduce because execution in debugger can modify how code being executed (avoiding condition 1 trying reproduce). race conditions 1 of major pitfalls in concurrent programming - making area deceptively difficult well.

in principle, best practice minimize use of shared resources , closely qualify how sharing coordinated when implementing concurrency. if object shared across multiple threads, should retained each of them ensure object stays in-scope while each thread completes processing.

apple has been taking steps simplify implementing concurrency. starting point familiarizing topic on ios. http://developer.apple.com/library/ios/#documentation/general/conceptual/concurrencyprogrammingguide/introduction/introduction.html%23//apple_ref/doc/uid/tp40008091

it's useful aware objective-c 2.0's properties can support atomic operations (and atomic default, nonatomic keyword disable default). http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/articles/ocproperties.html

and, old-school guide threads (out of favor approach, still useful background - sure familiar nslock). http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/multithreading/introduction/introduction.html%23//apple_ref/doc/uid/10000057i


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -