开发者

UIImageWriteToSavedPhotosAlbum with malloc_error

开发者 https://www.devze.com 2022-12-22 12:45 出处:网络
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.

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:didF开发者_开发百科inishSavingWithError: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];
}


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消