2

I am downloading images from a url using NSDATA and saving them to local file system using

NSData *dataForStorage = [NSData dataWithData:UIImagePNGRepresentation(img)];


    BOOL saveResult=[ dataForStorage writeToFile:jpegFilePath options:NSDataWritingAtomic error:&error];
    NSLog(@"Write returned error: %@", [error localizedDescription]);

My app crashes randomly without even giving a message, though some files are saved (again randomly). When I run the app in Debug mode, I frequently see "EXC_BAD_ACCESS" but continuing execution succeeds in saving some of the files.

This code is executed in background from:

[self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];

Please suggest.

Eimantas
  • 47,394
  • 15
  • 127
  • 164
  • 1
    can you post the crash log and the console log please? – Joze Apr 11 '11 at 11:02
  • Is it an iPhone app on iOS or a Mac app on MacOS? – Codo Apr 11 '11 at 11:08
  • This is iPad app. Here is the console log: 2011-04-11 16:36:59.784 AJiPadPhotos[5881:7203] *** __NSAutoreleaseNoPool(): Object 0x4e222c0 of class NSHTTPURLResponse autoreleased with no pool in place - just leaking 2011-04-11 16:36:59.784 AJiPadPhotos[5881:7203] *** __NSAutoreleaseNoPool(): Object 0x4b41c80 of class __NSCFData autoreleased with no pool in place - just leaking 2011-04-11 16:36:59.785 AJiPadPhotos[5881:7203] *** __NSAutoreleaseNoPool(): Object 0x4e23ba0 of class NSConcreteData autoreleased with no pool in place - just leaking – Monica Chaturvedi Apr 11 '11 at 11:09
  • @Monica, if it crashes you should get a stack trace. – Twelve47 Apr 11 '11 at 11:19
  • @Twelve47, it doesn't even reflect the error..no stack trace nothing...any clue where the problem might be? – Monica Chaturvedi Apr 11 '11 at 11:31
  • Is jpegFilePath valid? Has it been released already perhaps? – Twelve47 Apr 11 '11 at 11:32

3 Answers3

2

One of the problems in your code is that your running code in a thread without an autorelease pool but are using functions that would require one. Put the following code into the loadImageInBackground method:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// existing code

[pool drain];

This is probably just one of several problems. For further assistance, we need to see the stack trace of the crash.

Codo
  • 64,927
  • 16
  • 144
  • 182
1

Just a wild guess : arr is an autoreleased object, so, sometimes it gets deallocated before your selector gets called. Try using [arr copy] and release it after saving it.

Marcelo Alves
  • 1,856
  • 11
  • 12
0

I was having the EXACT same problem, but it turned out that the problem was something else: my URL was getting release prematurely. In the end this is what I did and it worked:

I made this call:

[self performSelectorInBackground:@selector(downloadData:) withObject:nil];

And this is the method:

// URL - (NSString) URL for file
// filePath - (NSString) save location on device
-(void)download:(NSString *)URL
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL]];
    [data writeToFile:filePath atomically:YES];
    [pool release];
}

So I think that your download code is correct, but there is some other variable that is getting deallocated early (possibly your path).

Hope this helps! I know the other answers on this page worked for me.

mtmurdock
  • 11,887
  • 20
  • 63
  • 102