views:

2214

answers:

3

Hi,

I'm using version 3.0.1 of the SDK.

With the iPhone connected to Instruments I'm getting a memory leak when I call UIImageWriteToSavedPhotosAlbum.

Below is my code:

    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);

and the selector method

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

    }

Am I forgetting to release something once the image has been saved and the selector method imageSavedToPhotosAlbum is called? Or is there a possible known issue with UIImageWriteToSavedPhotosAlbum?

Here is the stack trace from Instruments:

Leaked Object: GeneralBlock-3584
size: 3.50 KB

       30 MyApp start
      29 MyApp main /Users/user/Desktop/MyApp/main.m:14
      28 UIKit UIApplicationMain
      27 UIKit -[UIApplication _run]
      26 GraphicsServices GSEventRunModal
      25 CoreFoundation CFRunLoopRunInMode
      24 CoreFoundation CFRunLoopRunSpecific
      23 GraphicsServices PurpleEventCallback
      22 UIKit _UIApplicationHandleEvent
      21 UIKit -[UIApplication sendEvent:]
      20 UIKit -[UIWindow sendEvent:]
      19 UIKit -[UIWindow _sendTouchesForEvent:]
      18 UIKit -[UIControl touchesEnded:withEvent:]
      17 UIKit -[UIControl(Internal) _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(Internal) _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 Foundation +[NSThread detachNewThreadSelector:toTarget:withObject:]
       2 Foundation -[NSThread start]
       1 libSystem.B.dylib pthread_create
       0 libSystem.B.dylib malloc

I did a test with a new project and only added this code below in the 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);

The same leak shows up right after the app loads

Thank you for the help.

Bryan

A: 

A quick look doesn't reveal any objects that you own and need to release. Look through your code for places where you get a new object, alloc, copy or retain. Those are the objects you have to worry about releasing.

mahboudz
Thank you for the reply and info. I've checked my code and can't seem to find any thing I'm not releasing. I added the stack trace from instruments to my question above. The leak is in: GeneralBlock-3584 and the size is: 3.50 KBThanks again for your help.
stopmotion24
I did a test with a new project and only added this code below in the viewdidload:NSString *gnTmpStr = [NSString stringWithFormat:@"DefaultTest"];UIImage *ganTmpImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:gnTmpStr ofType:@"png"]]; // Request to save the image to camera rollUIImageWriteToSavedPhotosAlbum(ganTmpImage, nil, nil, nil);The same leak shows up right after app loads.
stopmotion24
I posted this issue on the Apple Developer forums here is link to that.https://devforums.apple.com/message/119423#119423
stopmotion24
A: 

I'm having the same problem with iPhone 3.1.

https://devforums.apple.com/message/119423#119423 check out this fourm post on apples dev site. Hope this helps.
stopmotion24
Thanks -- when I wrote that the devforums site was down, but later I was able to confirm that this is not a memory leak, just an occasion where Leaks is fooled.
A: 

I think I solved this one. It has to do with the contextInfo. Here's what I did and the memory leak no longer seems to appear:

Note that you have to define the object type of the 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; }

}

John Middendorf