PDA

View Full Version : EXC_BAD_ACCESS when trying to save image to app's /Documents folder




attodorov
Apr 17, 2008, 03:06 PM
Hi guys,

I am using the latest iphone SDK (Beta 3). I am getting an image from an URL, and then trying to save it via the NSData's writeToFile function to a file in the app's Documents folder. It crashes with EXC_BAD_ACCESS.

Do you have any idea why ? Thanks very much.

The code is the following:

==================== myappAppDelegate.m ==========

- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window makeKeyAndVisible];

NSFileManager *defaultManager;
defaultManager = [NSFileManager defaultManager];

NSString *filename = @"savedImage.jpg";

NSString *url = @"http://www.promovid.co.nz/images/ms_logo.jpg";

NSData *imageData;
NSURL *imageURL = [[NSURL alloc] initWithString:url];
if (imageURL) {
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
}

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
}

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:filename];

NSLog(appFile);

[imageData writeToFile:appFile atomically:YES];
}


=============================================



Littleodie914
Apr 17, 2008, 03:40 PM
Well, first, you're re-declaring your imageData pointer.

Have you used the debugger before? That might help you figure out which line of code is causing the problem.

If you haven't, try putting a bunch of NSLog()'s in your code to see where it's stopping. Example:


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLog(@"1"); // First change

NSString *documentsDirectory = [paths objectAtIndex:0];

NSLog(@"2"); // Second change

if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
}

NSString *appFile = [documentsDirectory stringByAppendingPathComponent:filename];
NSLog(@"3"); // Third change

gnasher729
Apr 17, 2008, 06:09 PM
Well, first, you're re-declaring your imageData pointer.

And that is the problem. There are two variables named imageData: The first one is never initialised, and that is the one that you try to write to the file. The second one exists only for the shortest possible amount of time; the data from the URL is assigned to it, then the variable is thrown away.

I think the compiler should have given a warning about that.

attodorov
Apr 18, 2008, 01:27 AM
Thanks very much guys, this was really a dumb mistake -:)

Littleodie914
Apr 24, 2008, 09:08 PM
I think the compiler should have given a warning about that.That's kind of what I was wondering too. :)

lee1210
Apr 24, 2008, 09:27 PM
That's kind of what I was wondering too. :)

it is in a different scope. If it were in the same scope it would be a redeclaration error.

-Lee

gnasher729
Apr 25, 2008, 03:42 AM
it is in a different scope. If it were in the same scope it would be a redeclaration error.

That's why it should have been a warning. It is completely legal Objective C, so it is not an error, but any programmer who spots it just _knows_ that this cannot possibly be what the author meant. That's what compilers give warnings for; things that are not illegal but likely mistakes.

There should also be a warning because the original variable was used uninitialised.