Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

namanhams

macrumors regular
Original poster
Jun 3, 2009
153
0
*Edited : nothing wrong with the below code. In the XIB file, i dump in a UIWebView. Never set any delegate, never do anything with that UIWebView. It just crashes. Removing the UIWebView, everything works fine again. I use Automatic Reference Counting, and i think there's a bug with UIWebView.

My app crashes and here's the message :
Code:
bool _WebTryThreadLock(bool), 0x8213c10: 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...

Here's what my code look like :

Code:
@implementation MyViewController 

- (void) viewDidLoad {
   [super viewDidLoad];
   [NSThread detachNewThreadSelector:@selector(doSmth) toTarget:self withObject:nil];
}

- (void) doSmth {
    // Do something here and don't update the UI
}

Here's the situations :
MyViewController is pushed into a navigation controller.
- When i go to MyViewController, stay there for a while until doSmth finishes. Then everything is fine.
- If i go to MyViewController, then quickly tap "Back", after a while (which i believe is when doSmth finishes), app crashes and it throws the message.


From what i understand, it should not be because of accessing UI from background thread, because :
- I'm sure i don't access UI in doSmth
- If it's the case, then app should crash for both situations.


So why it only crashes when i already tap "Back" button ?
 
Last edited:
You probably crash as something has called a method that has now been dealloced.

The most common one here is that you have some delegate you don't nil as you dealloc.

The other thing could well be that as you are doing stuff in a background thread, that thread is trying to call things in the viewcontroller that you just dealloc'ed when you pressed the back button.

Enable zombies for more information here.
 
When you hit the Back button the view controller is dealloced. If the code in doSmth is still running and it accesses ivars then you'll probably crash. Do you have a way to cancel that code?

I stay away from detachNewThreadSelector: I much prefer NSOperation. It is easily cancelled. If you use threads without understanding threading you will be hosed.

That _WebTryThreadLock is related to web views but is also related to drawing of text anywhere. You can google for more info.
 
I'm pretty sure it's not what you guys are saying.

I've created a new test project so that it isolates from my original project. In the new test project, i added only few lines of code as i already show in first post.

The behavior is consistent : if you drop a UIWebView into the XIB file, it crashes whenever you tap "Back" while the loading is still in progress. Throw away the UIWebView, it works again.

Here's how i go around this issue :

Code:
- (void) viewWillDisappear:(BOOL)animated {    
    // Is about to pop out
    if(self != self.navigationController.visibleViewController) {
        [_webview removeFromSuperview];
        _webview = nil;
    }
}


----------

When you hit the Back button the view controller is dealloced. If the code in doSmth is still running and it accesses ivars then you'll probably crash. Do you have a way to cancel that code?

I stay away from detachNewThreadSelector: I much prefer NSOperation. It is easily cancelled. If you use threads without understanding threading you will be hosed.

That _WebTryThreadLock is related to web views but is also related to drawing of text anywhere. You can google for more info.


I don't do much in doSmth. I even tried this but it's still crashes :

Code:
- (void) doSmth {
   @autorelease {
        // Do nothing
   }
}
 
Last edited by a moderator:
OK, you're hitting the Back button before the web view is finished loading. In your dealloc you should tell the web view to stopLoading and if you like set the delegate to nil.
 
OK, you're hitting the Back button before the web view is finished loading. In your dealloc you should tell the web view to stopLoading and if you like set the delegate to nil.

No. I just dump the webview into the Xib file. That's it. I dont touch it at all.
 
Maybe I don't understand, but this sounds like the classic problem of a webview having a delegate that has been dealloced. When you set everything up does your webview have a delegate? Before that delegate goes away do you set the webview delegate property to nil?

Also, does it crash because of a segfault? An exception? The actual error message will point you in the right direction.
 
Have you tried to enable zombies yet? Doesn't crash then does it? ;)

make sure to nil everything in your dealloc.

(and use properties!)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.