【问题标题】:UIImageWriteToSavedPhotosAlbum with malloc_errorUIImageWriteToSavedPhotosAlbum 与 malloc_error
【发布时间】:2010-03-08 21:49:09
【问题描述】:

我有一个带有按钮的 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];
}

【问题讨论】:

    标签: iphone malloc wallpaper


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      UIImageWriteToSavedPhotosAlbum 异步执行保存,这意味着您必须确保您的UIImage 始终保持不变。您正在向它传递一个自动释放的对象,因此它有时会在尝试保存时崩溃。将setWallpaper: 更改为将retain 发送到UIImage。然后你可以在你的回调中release 或autorelease 避免泄漏。一个例子:

      更改获取图像的行:

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

      然后添加

      [image release];
      

      在回调中。

      【讨论】:

      • 我这样做了...但仍然收到相同的错误...顺便说一句,我从 malloc 收到两条错误消息。两者完全相同。现在我创建了一个空项目,并复制了我的 ViewController + NIB,它在那里工作得很好......我会检查它添加到项目中的任何框架或库是否会导致错误......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多