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 902069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:39:29+00:00 2026-05-15T15:39:29+00:00

I’ve come across an issue with using a third party library, and am not

  • 0

I’ve come across an issue with using a third party library, and am not sure what the common pattern to solve it is.

I’m using the asi-http-request class, which fetches http objects asynchronously using a thread.

In my objects dealloc() method, I do

[request setDelegate:nil];
[request release];

However the delegate is sometimes still called after this has happened. (I can see when this happens the delegate field of the request object is nil.) This sometimes causes a crash if the delegate has been destroyed already.

I believe this is a race condition. The code from ASIHTTPRequest that calls the delegate looks like this:

// Let the delegate know we are done
if ([self didFinishSelector] && [[self delegate] respondsToSelector:[self didFinishSelector]]) {
    [[self delegate] performSelectorOnMainThread:[self didFinishSelector] withObject:self waitUntilDone:[NSThread isMainThread]];
}

The problem happens if the performerSelectorOnMainThread has been called (but not completed) when the setDelegate call happens on the main thread.

One solution would be to add a wrapper around ‘didFinishSelector’ that checks (on the main thread) that the delegate is still non-nil before calling the selector, but this would result in a lot of wrappers.

There is some background here:

http://groups.google.com/group/asihttprequest/browse_thread/thread/721220b9645f4a42

All suggestions on the “normal” solution for this appreciated!

Thanks

Joseph

  • 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-15T15:39:30+00:00Added an answer on May 15, 2026 at 3:39 pm

    My original thoughts (wrapper around ‘didFinishSelector’ that checks on the main thread that the delegate is still non-nil before calling the selector) turned out to be the correct solution, as confirmed by helpful folks over on the apple dev forums:

    https://devforums.apple.com/message/255935#255935

    To avoid my worry of ending up with lots of wrappers, I managed to create only a single wrapper:

    - (void)callSelectorCallback:(SEL *)selectorPtr withTarget:(id *)targetPtr
    {
        id target = *targetPtr;
        SEL selector = *selectorPtr;
    
        if (!selector || !target)
            return;
    
        if ([target respondsToSelector:selector])
        {
            [target performSelector:selector withObject:self];
        }
    }
    
    - (void)callSelector:(SEL *)selector withDelegate:(id *)target
    {
        if (!*selector || !*target)
            return;
    
        SEL callback = @selector(callSelectorCallback:withTarget:);
        NSMethodSignature *signature = [ASIHTTPRequest instanceMethodSignatureForSelector:callback];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
        [invocation setSelector:callback];
        [invocation setTarget:self];
        [invocation setArgument:&selector atIndex:2];
        [invocation setArgument:&target atIndex:3];
    
        [invocation performSelectorOnMainThread:@selector(invoke) withObject:nil waitUntilDone:[NSThread isMainThread]];
    }
    

    then when I want to call the delegate, the code just looks something like this:

    [self callSelector:&didFinishSelector withDelegate:&delegate];
    

    As best I can tell from experiments & code analysis (and assuming setDelegate is only called from the main thread), this is 100% safe. It could be made safe for the non-main thread calls to setDelegate by taking the object lock inside callSelectorCallback.

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

Sidebar

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.