IPhone application crashes when user exits UITableView with MBProgressHUD launched in a separate thread

In my application, I have a UITableViewController that loads calculated data from my master data store. This can take quite a while and hang the main thread, so to give the user visual feedback, I installed the MBProgressHUD widget, which will show a progress indicator (only the spinning wheel), while the calculation method works in a separate thread.

This is great, except that the user can still exit the UITableViewController if he considers that it takes too long. Of course, this is good, except that when a separate thread completes its work, it still tries to call its delegate (UITableViewController) to hide the MBProgressHUD. This results in a crash, since the user has already left the UITableViewController, he has dealloc 'd and release ' d.

MBProgressHUD has the following code to stop this:

 if(delegate != nil && [delegate conformsToProtocol:@protocol(MBProgressHUDDelegate)]) { if([delegate respondsToSelector:@selector(hudWasHidden)]) { [delegate performSelector:@selector(hudWasHidden)]; } } 

However, my application somehow still works with this internal code ( [delegate performSelector:@selector(hudWasHidden)] ), even though the UITableViewController completely disappeared - this caused the application to crash.

Any suggestions? I do not work with NSZombiesEnabled .

+4
source share
2 answers

From your view, UITableViewControllerWillDisappear, viewDidDisappear or dealloc, set MBProgressHUD.delegate = nil;

+4
source

Once the user has exited the table controller, the hud delegation property points to the freed object (= memory zone, which can contain anything). This causes a crash at the end of the compute stream and attempts to send a message to the delegate.

In the dealloc table view controller, you must set the hud delegate to nil .

0
source

Source: https://habr.com/ru/post/1344836/


All Articles