3 ms·
This is a hack, not a general purpose solution, as it requires modifying the class, and and still must be done carefully to avoid accidental retain/release. Ca
by nupark2 15y ago
This is a hack, not a general purpose solution, as it requires modifying the class, and and still must be done carefully to avoid accidental retain/release.
Calling retain/release from a background thread will happen simply by using blocks, gcd, or performSelector: APIs, as designed, to attempt to schedule execution on the main thread. Avoiding the issue requires exquisite care, unless you actually want to modify every single class that you may access an instance of from a background thread (such as GCD).
As to the first point, it applies in a NON-GC environment, where you hold weak references to delegates a(nd deregister them in dealloc.)
- xsmasher 15y agoSorry, the term "weak reference" threw me. So you mean you have pointers to objects you have not retained, and you "deregister" (set them to nil?) in your dealloc? I don't see how that can trigger a race condition or call anyone else's dealloc. You may have a valid point but I don't grasp it.
- nupark2 15y agoThink delegates, or observers. There is a race condition between calling the delegate and deregistration of the delegate. Once an object's dealloc method has been called, you must not resurrect it, but there is a race between the execution of -dealloc, the de-registration of the delegate, and the dispatch of messages to the delegate.
- xsmasher 15y agoSo you're setting threadedObject.delegate = nil in your delegate object's dealloc, but the threadedObject is sending subsequent messages to your delegate? The doesn't sounds like a retain-release problem. Wouldn't the same thing happen when setting threadedObject.delegate=foo? Messages could still get send to the old delegate if you haven't done your synchronization right.
- nupark2 15y agoMissing the forest for the trees. The problem is that the old delegate will be invalidated before it deregisters itself, because it will not deregister itself until it is invalid (during -dealloc)