2009-09-05 47 views
1

我正在使用SDK 3.0.1版。UIImageWriteToSavedPhotosAlbum顯示iPhone連接到儀器的內存泄漏

隨着連接到儀器的iPhone,當我打電話給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可能已知的問題?

這裏是從儀器的堆棧跟蹤:

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

我做了一個新的項目的測試,只有加入下面這段代碼在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); 

同樣的泄漏顯示了正確的應用程序加載後

感謝您的幫助。

布萊恩

回答

0

快速瀏覽一下沒有發現任何你擁有的任何對象,需要釋放。仔細查看代碼,瞭解獲取新對象,分配,複製或保留的位置。這些是你必須擔心釋放的物體。

+0

謝謝你的回覆和信息。我檢查了我的代碼,似乎無法找到任何我沒有發佈的東西。我從儀器添加了堆棧跟蹤到上面的問題。泄漏位於:GeneralBlock-3584,大小爲:3.50 KB 再次感謝您的幫助。 – stopmotion24 2009-09-06 03:03:02

+0

我做了一個新項目的測試,只在viewdidload下面添加下面的代碼: NSString * gnTmpStr = [NSString stringWithFormat:@「DefaultTest」]; UIImage * ganTmpImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:gnTmpStr ofType:@「png」]]; \t //請求將圖像保存到相機膠捲 UIImageWriteToSavedPhotosAlbum(ganTmpImage,nil,nil,nil); 應用程序加載後出現相同的泄漏。 – stopmotion24 2009-09-06 04:38:32

+0

我在蘋果開發者論壇上發佈了這個問題,這裏有鏈接。 https://devforums.apple.com/message/119423#119423 – stopmotion24 2009-09-07 02:43:09

1

我想我解決了這個問題。它與contextInfo有關。下面是我沒有和內存泄漏似乎不再出現:

請注意,您必須定義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; 
} 

}