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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:39:34+00:00 2026-05-16T01:39:34+00:00

I have a small iPhone app which has a button on the first view.

  • 0

I have a small iPhone app which has a button on the first view. When I select this button I load up my new view which has an image on it. I’m currently using the following code to load the image from an online source on a separate thread, whilst allowing the user to continue controlling the application:

- (void) loadImageTest
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSURL *url = [[NSURL alloc] init];
    url = [NSURL URLWithString:logoPath];

    NSData *data = [[NSData alloc] init];
    data = [NSData dataWithContentsOfURL:url];

    loadingImage = [UIImage imageWithData:data];
    titleLogoImage.image = loadingImage;

    //[pool drain];
    [pool release];
}

This is called from this line of code in the new view’s init:

[NSThread detachNewThreadSelector:@selector(loadImageTest) toTarget:self withObject:nil];

Now this works fine (ish), but if I close the new view and then load a new one up again in quick succession (or just after-wards in some cases) it will bomb out with the classic EXC_BAD_ACCESS. I’m sure that this is due to the code within the thread pool, but can anyone see why this is happening?

Thanks

  • 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-16T01:39:35+00:00Added an answer on May 16, 2026 at 1:39 am

    Yours:

    // This is ok, you might try using NSURLConnections asynchronous methods instead of making
    // your own thread.
    [NSThread detachNewThreadSelector:@selector(loadImageTest) toTarget:self withObject:nil];

    - (void)loadImageTest
    {
        // This is fine
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        // you're making and then abandoning this url object so it will leak
        NSURL *url = [[NSURL alloc] init];
        // this is fine
        url = [NSURL URLWithString:logoPath];
        // again making and abandoning an object
        NSData *data = [[NSData alloc] init];
        data = [NSData dataWithContentsOfURL:url];
        // this works, but is not thread safe,
        // can't operate on UIKit objects off the
        // main thread
        loadingImage = [UIImage imageWithData:data];
        titleLogoImage.image = loadingImage;
        // this is fine
        //[pool drain];
        [pool release];
    }
    

    Cleaned up to make things thread safe, etc. Should help:

    // I'm assuming you have a iVar for logoPath but we don't want to
    // make threaded calls to an iVar (it's not mutable, so you could do it, but it's just bad form)
    // If i'm wrong about logoPath being an iVar don't sweat copying it.
    - (void)whateverMethodYouWant
    {
        NSString *aLogoPath = [[logoPath copy] autorelease];
        [NSThread detachNewThreadSelector:@selector(loadImageForPath:) toTarget:self withObject:aLogoPath];
    }
    - (void)loadImageForPath:(NSString *)aLogoPath
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:aLogoPath]];
        // the performSelector will retain the data until the method can be performed
        [self performSelectorOnMainThread:@selector(setImageForTitleLogo:) withObject:imgData waitUntilDone:NO];
    
        [pool release];
    }
    - (void)setImageForTitleLogo:(NSData *)imgData
    {
        // This part is not strictly necessary
        // but it's a nice way to wrap a method that needs to happen on the main thread.
        if ([NSThread isMainThread])
        {
            // make the image (i'm assuming you meant loadingImage to be a local scope variable)
            UIImage *loadingImage = [UIImage imageWithData:imgData];
            // make sure the button still exists 
            // also make sure you're setting any references to this button to nil when you're releasing and making new views
            // so that you're not addressing a button that's been released
            // (assigning the image to the button will cause the button to retain it for you)
            if (titleLogoImage != nil)
                titleLogoImage.image = loadingImage;
        }
        else
        {
            [self performSelectorOnMainThread:@selector(setImageForTitleLogo:) withObject:imgData waitUntilDone:NO];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small iPhone app which I've created a button with some functionality
I have a small level based iPhone app. I need to load and release
I have an iPhone App with a view which contains 2 buttons. I created
I have what I would consider a small sized iPhone app that uses SQLite.
I'm building a MonoTouch iPhone app, but have come up with a small issue.
I have a small iPhone Project with a UITextView on my View, designed in
I am new to iphone.I have a small doubt (i.e),I have create a table
I have a simple iPhone app which accesses some remote data on start-up then
I have a small IPhone app that I am working on and I am
I have created a small iPhone app using MonoTouch - got all provisioning files

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.