Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6553593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:36:02+00:00 2026-05-25T12:36:02+00:00

This is by far the weirdest problem I’ve been stuck with. I have a

  • 0

This is by far the weirdest problem I’ve been stuck with.

I have a UIViewController on a UINavigationController and I want to call a method at viewDidAppear using NSInvocationOperation so it can run on a back thread when the view becomes visible.

The problem is that if I pop the view controller BEFORE the operation (in this case the testMethod method) completes running, the app crashes.

Everything works fine if I pop the view controller AFTER the operation runs it’s course.

When the app crashes, it stops at [super dealloc] with “EXC-BAD-ACCESS” and gives me the following error.

bool _WebTryThreadLock(bool), xxxxxxxxx: 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…

And this is my code (super simplified)..

- (void)viewDidAppear:(BOOL)animated
{
     [super viewDidAppear:animated];

    NSInvocationOperation *theOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testMethod) object:nil];
    [operationQueue addOperation:theOperation];
    [theOperation release];
}

- (void)testMethod
{
    NSLog(@"do some stuff that takes a few seconds to complete");
}

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

The testMethod has some code that takes a few seconds to complete. I only have a few clues and I really don’t know how and where to start debugging this.

  • Clue #1: The funniest thing is that if I remove the [_tableView release]; from dealloc then the app doesn’t crash. But of course this would cause a leak and I can’t remove it.

  • Clue #2: I’ve tested this code on a separate “clean” UIViewController with a UITableView and to my surprise it didn’t crash.

  • Clue #3: The app doesn’t crash is the UITableView’s datasource is set to nil in viewDidLoad.

  • Clue #4: The app doesn’t seem crash if I use the same code in viewDidAppear somewhere else like an IBAction.

  • Clue #5: I’ve tried looking over stack data with NSZombie but it gives me tons of data and it leads me nowhere.

I have some very complicated code within my UITableViewDelegate and UITableViewDataSource and I really don’t know where to start debugging this. I really hope I don’t have to go through line by line or rewrite the entire thing because of this.

Any pointers on where I should be looking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:36:02+00:00Added an answer on May 25, 2026 at 12:36 pm

    The problem is likely that your view controller’s last reference is the operation queue holding onto it, which means you are technically calling (or having the system call) some UIKit methods in a background thread (a big no-no) when the operation cleans up.

    To prevent this from happening, you need to send a keep-alive message to your controller on the main thread at the end of your operation, by adding something like this to the last line in your testMethod:

    [self performSelectorOnMainThread:@selector(description) withObject:nil waitUntilDone:NO];
    

    There is still a chance that this message may get processed before the operation queue releases your view controller, but that chance is pretty remote. If it’s still happening, you could do something like this:

    [self performSelectorOnMainThread:@selector(keepAlive:) 
                           withObject:[NSNumber numberWithBool:YES]
                        waitUntilDone:NO];
    
    - (void)keepAlive:(NSNumber *)fromBackground
    {
        if (fromBackground)
             [self performSelector:@selector(keepAlive:) withObject:nil afterDelay:1];
    }
    

    By sending a message to your view controller on the main thread, it will keep the object alive (NSObject retains your view controller until the main thread handles the message). It will also keep the view controller alive if you perform a selector after a delay.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking for solutions to this problem for far too long considering how
I am porting an Oracle function into Postgres PGPLSQL.. I have been using this
So this is by far the weirdest crash I have seen yet. This is
Okay, this is by far the weirdest bug I have ever encountered. It's pretty
I'm using v4.0. Update: I've hacked together this so far: public class AllFilter :
I have this code so far which perfectly but relies on there being a
I have had no luck with this task so far so grateful for any
This is what I have so far: myArray.map!{ rand(max) } Obviously, however, sometimes the
I have (hacked) this regex this far which matches any words between hyphens and
I've been thinking about this far too long and haven't gotten any idea, maybe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.