【问题标题】:UIImageWriteToSavedPhotosAlbum showing memory leak with iPhone connected to InstrumentsUIImageWriteToSavedPhotosAlbum 显示 iPhone 连接到 Instruments 时出现内存泄漏
【发布时间】:2009-09-05 06:09:47
【问题描述】:

我使用的是 3.0.1 版的 SDK。

iPhone 连接到 Instruments 时,我在调用 UIImageWriteToSavedPhotosAlbum 时遇到内存泄漏。

下面是我的代码:

    NSString *gnTmpStr = [NSString stringWithFormat:@"%d", count];

UIImage *ganTmpImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:gnTmpStr ofType:@"jpg"]];

// Request to save the image to camera roll
UIImageWriteToSavedPhotosAlbum(ganTmpImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);

和选择器方法

        - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSString *message;
 NSString *title;
 if (!error)
 {
  title = @"Wallpaper";
  message = @"Wallpaper Saved";

 }
 else
 {
  title = @"Error";
  message = [error description];

 }
 UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:title
        message:message 
        delegate:self
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
 [alert show];
 [alert release];

    }

图像保存后我是否忘记发布某些内容并调用了选择器方法 imageSavedToPhotosAlbum?还是 UIImageWriteToSavedPhotosAlbum 可能存在已知问题?

这是 Instruments 的堆栈跟踪:

泄露对象:GeneralBlock-3584
大小:3.50 KB

       30 MyApp 启动
      29 MyApp 主 /Users/user/Desktop/MyApp/main.m:14
      28 UIKit UIApplicationMain
      27 UIKit-[UIApplication_run]
      26 图形服务 GSEventRunModal
      25 CoreFoundation CFRunLoopRunInMode
      24 CoreFoundation CFRunLoopRunSpecific
      23 GraphicsServices PurpleEventCallback
      22 UIKit_UIApplicationHandleEvent
      21 UIKit-[UIApplication 发送事件:]
      20 UIKit-[UIWindow 发送事件:]
      19 UIKit-[UIWindow_sendTouchesForEvent:]
      18 UIKit-[UIControl touchesEnded:withEvent:]
      17 UIKit-[UIControl(内部)_sendActionsForEvents:withEvent:]
      16 UIKit-[UIControl sendAction:to:forEvent:]
      15 UIKit-[UIApplication sendAction:toTarget:fromSender:forEvent:]
      14 UIKit-[UIApplication sendAction:to:from:forEvent:]
      13 CoreFoundation-[NSObject performSelector:withObject:withObject:]
      12 UIKit-[UIBarButtonItem(内部)_sendAction:withEvent:]
      11 UIKit-[UIApplication sendAction:to:from:forEvent:]
      10 CoreFoundation -[NSObject performSelector:withObject:withObject:]
       9 MyApp -[FlipsideViewController svPhoto] /Users/user/Desktop/MyApp/Classes/FlipsideViewController.m:218
       8 0x317fa528
       7 0x317e3628
       6 0x317e3730
       5 0x317edda4
       4 0x3180fc74
       3 基础+[NSThread detachNewThreadSelector:toTarget:withObject:]
       2 基础-[NSThread 开始]
       1 libSystem.B.dylib pthread_create
       0 libSystem.B.dylib malloc

我用一个新项目做了一个测试,只在viewDidLoad下面添加了这段代码:

NSString *gnTmpStr = [NSString stringWithFormat:@"DefaultTest"];
UIImage *ganTmpImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:gnTmpStr ofType:@"png"]];
// Request to save the image to camera roll 
UIImageWriteToSavedPhotosAlbum(ganTmpImage, nil, nil, nil); 

应用加载后立即显示相同的泄漏

感谢您的帮助。

布莱恩

【问题讨论】:

    标签: iphone memory iphone-sdk-3.0 memory-leaks


    【解决方案1】:

    我想我解决了这个问题。它与上下文信息有关。这是我所做的,似乎不再出现内存泄漏:

    注意,你必须定义 contextInfo 的对象类型...

    -(IBAction)savePhoto{
    
     UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:imageName], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    //MEMORY LEAK HERE
    }
    
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(NSDictionary *)contextInfo {  
        if (error != NULL){
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Image was not saved, sorry" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [alert show];
      [alert release];
        }
        else { //no errors
    
    
      //tell the user photo ok:
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:contents message:@"Image was saved to your photos" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [alert show];
      [alert release];
        }
     if (image !=NULL){
     [image release];
     image=nil;
     }
     if(contextInfo !=NULL){
      [contextInfo release];
      contextInfo=nil;
     }
    
    }
    

    【讨论】:

      【解决方案2】:

      快速浏览不会显示您拥有并需要释放的任何对象。查看您的代码,寻找获得新对象、分配、复制或保留的位置。这些是您必须担心释放的对象。

      【讨论】:

      • 感谢您的回复和信息。我检查了我的代码,似乎找不到任何我没有发布的东西。我将仪器的堆栈跟踪添加到我上面的问题中。泄漏位于:GeneralBlock-3584,大小为:3.50 KB 再次感谢您的帮助。
      • 我对一个新项目进行了测试,只在 viewdidload 中添加了以下代码: NSString *gnTmpStr = [NSString stringWithFormat:@"DefaultTest"]; UIImage *ganTmpImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:gnTmpStr ofType:@"png"]]; // 请求将图像保存到相机胶卷 UIImageWriteToSavedPhotosAlbum(ganTmpImage, nil, nil, nil);应用加载后立即出现相同的泄漏。
      • 我在 Apple Developer 论坛上发布了这个问题,这里有指向该问题的链接。 devforums.apple.com/message/119423#119423
      猜你喜欢
      • 2011-10-10
      • 2010-12-01
      • 1970-01-01
      • 2016-01-10
      • 2011-08-13
      • 2017-05-26
      • 2011-08-17
      • 2011-06-28
      • 2011-06-07
      相关资源
      最近更新 更多