【发布时间】: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