Is it possible to [self save] within -dealloc? Or, how can I ensure that dealloc happens in the main thread?

Here is my situation. It got complicated, so carry me.

I have a view class, let's call it MyView . It creates a subset of the download indicator, and then launches a background mode that will load data. It also creates a block in which the background queue operation in the main queue will be executed. The block prepares the view by adding another subview, a UITextView , with the loaded data. Of course, for this, the block must have a link to the view.

Thus, the background operation saves the block, and the block saves the view. Still with me?

Sometimes a MyView instance is removed from its supervisor before MyView in the background. And sometimes the operation of the main queue that calls the block is completely cleared before the operation of the background queue is completely cleared. In this case, the MyView instance may receive its -dealloc call in the background thread because the last link to the view belonged to the block, and the last link to the block belonged to the background operation.

UIKit does not like to be called from any thread except the main thread. In the case of a UITextView , it is obvious that it even includes calls to -dealloc . I get EXC_BAD_ACCESS from what is called a "web stream lock" during a -dealloc text view.

I think it is wise to use the last link in the background thread, and I would like to handle this from my implementation of -dealloc , for example:

 - (void)dealloc { if ([NSOperationQueue currentQueue] == [NSOperationQueue mainQueue]) { // The usual -- dealloc subviews safely on the main thread self.myIvar = nil; [super dealloc]; } else { // Not on the main thread, so keep the object alive // in spite of the dealloc call. [self retain]; // explicit retain [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // implicit retain at block creation [self release]; // explicit release }]; // implicit release, dealloc called again, but on the main thread } } 

Therefore, when you call -release on an object, the implementation in NSObject calls -dealloc if the save count reaches zero. Is that all that happens? In other words, is it okay to call -dealloc and not call super ? Am I making some disgusting zombie object or is it fine?

If this is not the case, then what good way to make sure -dealloc is called in the main thread?

+4
source share
4 answers

Why not just override the release?

 - (void)release { if (![NSThread isMainThread]) { [self performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO]; } else { [super release]; } } 

Edit: This was a preliminary ARC. Do not do this with ARC.

+5
source

I saw this approach:

 - (void)dealloc { if (is on main thread) { [self _reallyDealloc]; } else { [self performSelectorOnMainThread:@selector(_reallyDealloc)]; } } - (void)_reallyDealloc { [ivar release]; [super dealloc] } 

It's still not perfect (and breaks down in ARC). The best answer is to ensure that the final release will occur in the main thread.

pardon pseudo code. IPhone input code is not optimal

+1
source

Why not just skip passing your view to the block that was running in the background thread? You can have a controller register for notification, and the block issues a notification in the notification center of the main thread:

 NSNotification *notification = [NSNotificationnotificationWithName:@"data upated" object:nil]; [[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:NO]; 
+1
source

After calling dealloc, it's too late. This object will go away, no matter what you do. If the problem is that dealloc is doing what is unsafe to do in the background thread, then dealloc can send these things to the main queue, where they will be executed when the "I" has already left.

The override "save" is pretty perverse. Say this is not what you should do when you ask for advice on the overflow.com stack. And then there is ARC where you cannot do this.

+1
source

All Articles