EXC_BAD_ACCESS when trying to save image to app's /Documents folder

Discussion in 'Mac Programming' started by attodorov, Apr 17, 2008.

  1. macrumors newbie

    Joined:
    Apr 17, 2008
    #1
    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];
    }


    =============================================
     
  2. macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #2
    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:

    Code:
    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
    
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Apr 17, 2008
    #4
    Thanks very much guys, this was really a dumb mistake -:)
     
  5. macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #5
    That's kind of what I was wondering too. :)
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    it is in a different scope. If it were in the same scope it would be a redeclaration error.

    -Lee
     
  7. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #7
    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.
     

Share This Page