Strange crash : never access UI from background thread

Discussion in 'iOS Programming' started by namanhams, Jan 15, 2012.

  1. namanhams, Jan 15, 2012
    Last edited: Jan 15, 2012

    namanhams macrumors regular

    Joined:
    Jun 3, 2009
    #1
    *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 ?
     
  2. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #2
    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.
     
  3. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. namanhams, Jan 15, 2012
    Last edited by a moderator: Jan 16, 2012

    namanhams thread starter macrumors regular

    Joined:
    Jun 3, 2009
    #4
    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;
        }
    }
    


    ----------


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

    Code:
    - (void) doSmth {
       @autorelease {
            // Do nothing
       }
    }
     
  5. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    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.
     
  6. namanhams thread starter macrumors regular

    Joined:
    Jun 3, 2009
    #6
    No. I just dump the webview into the Xib file. That's it. I dont touch it at all.
     
  7. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #7
    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.
     
  8. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #8
    Have you tried to enable zombies yet? Doesn't crash then does it? ;)

    make sure to nil everything in your dealloc.

    (and use properties!)
     

Share This Page