views:

207

answers:

1

So I have the following code in a method which I want to set a UIImageView image to that of one from an online source:

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

Then in the method called by the thread I have this:

- (void) loadImage
{
    NSURL *url = [NSURL URLWithString:logoPath]; // logoPath is an NSString with path details
    NSData *data = [NSData dataWithContentsOfURL:url];

    logoImage.image = [UIImage imageWithData:data];
}

This works great however I get many warnings within the Debugger Console along the lines of:

2010-05-10 14:30:14.052 ProjectTitle[2930:633f] * _NSAutoreleaseNoPool(): Object 0x169d30 of class NSHTTPURLResponse autoreleased with no pool in place - just leaking

This occurs many times each time I call the new thread and then eventually, under no pattern, after calling a few of these threads I get the classic 'EXC_BAD_ACCESS' run-time error.

I understand that this is happening because I'm not retaining the object but how can I solve this with the code in 'loadImage' shown above?

Thanks

+1  A: 

You need to create an autorelease pool for the thread otherwise objects which you do not explicitly release will not get released. See the Apple Docs, which essentially tell you to do the following:

- (void)myThreadMainRoutine
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool

  // Do thread work here.

  [pool release];  // Release the objects in the pool.
}
pheelicks
Excellent thanks :)
ing0