views:

408

answers:

2

I have an NIB file with a button. When I click this button, the setWallpaper: selector is called. Everything works as expected (the image is saved), excepte by the error thrown by malloc.

malloc: *** error for object 0x184d000: pointer being freed was not allocated ***
set a breakpoint in malloc_error_break to debug

I've set a breakpoint at malloc_error_break, but I don't understand anything from the debugger. I couldn't even find the object 0x184d000. Does anyone know why is this happening? I had also tried to retain the UIImage before sending it to UIImageWriteToSavedPhotosAlbum, but without success.

My code is below:

- (IBAction)setWallpaper:(id)sender {
    UIImage *image = [UIImage imageNamed:@"wallpaper_01.png"];
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    UIAlertView *alertView = [[UIAlertView alloc] 
          initWithTitle:NSLocalizedString(@"Galo!!!",@"Saved image message: title")
                message:NSLocalizedString(@"Now, check your \"saved photos\" group at \"photos\" app in your iPhone and select the actions menu > set as wallpaper.",@"Saved image message")
               delegate:nil
      cancelButtonTitle:NSLocalizedString(@"OK",@"OK Button")
      otherButtonTitles:nil];
   [alertView show];
   [alertView release];
}
A: 

UIImageWriteToSavedPhotosAlbum does the saving asynchronously, which means you have to make sure your UIImage stays around for that whole time. You're passing it an autoreleased object, so it's crashing sometime out there trying to do the save. Change setWallpaper: to send retain to the UIImage. Then you can release or autorelease it in your callback to avoid leaking. An example:

Change the line that gets the image:

UIImage *image = [[UIImage imageNamed:@"wallpaper_01.png"] retain];

Then add

[image release];

in the callback.

Carl Norum
I did that... and still getting the same error... By the way, I get two error messages from the malloc. Both exactly the same. Now I created an empty project, and copied my ViewController + NIB and it worked fine there... I'll check it any framework or lib added to the project is causing a bug...
+2  A: 

Ok, after cloning almost my entire project, I realized that the problem comes from OS3.0. Changed to OS3.1 and everything works just fine. Thanks for the help, Carl!