Swissdude
Swissdude

Reputation: 3556

removing autoreleased UIWebview results in crash

I have a view containing a subview which in turn contains a UIWebview.

the subview is allocated and autoreleased

theController = [[[viewContainer alloc] initWithNibName:@"viewContainer" bundle:[NSBundle mainBundle]]  autorelease];

The content for the webview is loaded onViewDidLoad in the subview.

I can remove this subview with no problem - as long as the app didn't go to the background before! The webview contains a link - when the user clicks on it, the app goes to the background and Safari opens the link. So far, so good. When I return to the app now and try to remove the subview containing the webview, I get this error:

bool _WebTryThreadLock(bool), 0x7f3e970: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

[Edit]

Didn't really find a solution, just a mere workaround (thanks to Jumhyn for the help!):

I added an NSNotification to the subview that holds the webview. When I get the applicationDidBecomeActive-Notification, I just use the same NSURLrequest again that I use in the viewDidLoadMethod and reload the content of the webview. Now I can safely remove the view without getting an error...

Very weird behaviour...

Upvotes: 0

Views: 228

Answers (1)

Jumhyn
Jumhyn

Reputation: 6777

You really only want to use autorelease when you arent going to be using the object for an extended period of time. Instead, use an instance variable to hold your view controller, and release it in the dealloc method. Also, you can convert your project to ARC, which removes the need for retain and release calls entirely.

Upvotes: 1

Related Questions