1

我有一个带有按钮的 NIB 文件。当我单击此按钮时,将调用 setWallpaper: 选择器。一切都按预期工作(图像已保存),除了 malloc 引发的错误。

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

我在 malloc_error_break 处设置了一个断点,但我对调试器一无所知。我什至找不到对象 0x184d000。有谁知道为什么会这样?在将 UIImage 发送到 UIImageWriteToSavedPhotosAlbum 之前,我也曾尝试保留它,但没有成功。

我的代码如下:

- (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];
}
4

2 回答 2

2

好的,在克隆了我几乎整个项目之后,我意识到问题来自于 OS3.0。更改为 OS3.1,一切正常。谢谢你的帮助,卡尔!

于 2010-03-08T22:36:24.427 回答
0

UIImageWriteToSavedPhotosAlbum异步进行保存,这意味着您必须确保您UIImage一直在身边。您正在向它传递一个自动释放的对象,因此它有时会在尝试保存时崩溃。更改setWallpaper:为发送retainUIImage. 然后你可以releaseautorelease你的回调中使用它来避免泄漏。一个例子:

更改获取图像的行:

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

然后加

[image release];

在回调中。

于 2010-03-08T21:54:25.870 回答