UIImageWriteToSavedPhotosAlbum显示iPhone连接到仪器时内存泄漏
我使用的是SDK的3.0.1版 当iPhone连接到仪器时,当我调用UIImageWriteToSavedPhotosAlbum时,内存泄漏 下面是我的代码:UIImageWriteToSavedPhotosAlbum显示iPhone连接到仪器时内存泄漏,iphone,memory,iphone-sdk-3.0,memory-leaks,Iphone,Memory,Iphone Sdk 3.0,Memory Leaks,我使用的是SDK的3.0.1版 当iPhone连接到仪器时,当我调用UIImageWriteToSavedPhotosAlbum时,内存泄漏 下面是我的代码: NSString *gnTmpStr = [NSString stringWithFormat:@"%d", count]; UIImage *ganTmpImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:gnTmpSt
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];
}
一旦图像被保存并且调用了选择器方法ImageSavedPhotosAlbum,我是否忘记发布一些内容?或者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
同样的漏洞在应用程序加载后立即出现
谢谢你的帮助
布莱恩(Bryan)快速查看不会发现任何你拥有的需要释放的物品。在代码中查找获得新对象、alloc、copy或retain的位置。这些是您必须担心释放的对象。快速查看并不会显示您拥有的任何对象以及需要释放的对象。在代码中查找获得新对象、alloc、copy或retain的位置。这些都是你必须担心释放的对象。我想我解决了这个问题。它与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;
}
}
我想我解决了这个问题。它与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;
}
}
谢谢你的回复和信息。我已经检查了我的代码,似乎找不到任何我没有发布的东西。我在上面的问题中添加了来自仪器的堆栈跟踪。泄漏位于:GeneralBlock-3584中,大小为:3.50 KB再次感谢您的帮助。我对一个新项目进行了测试,只在viewdidload中添加了以下代码:NSString*gnTmpStr=[NSString stringWithFormat:@“DefaultTest”];UIImage*ganTmpImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:gnTmpStr的类型:@“png”];//请求将图像保存到相机卷UIImageWriteToSavedPhotosAlbum(甘特图,nil,nil,nil);同样的漏洞在应用程序加载后立即出现。我在苹果开发者论坛上发布了这个问题,这里是链接。谢谢你的回复和信息。我已经检查了我的代码,似乎找不到任何我没有发布的东西。我在上面的问题中添加了来自仪器的堆栈跟踪。泄漏位于:GeneralBlock-3584中,大小为:3.50 KB再次感谢您的帮助。我对一个新项目进行了测试,只在viewdidload中添加了以下代码:NSString*gnTmpStr=[NSString stringWithFormat:@“DefaultTest”];UIImage*ganTmpImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:gnTmpStr的类型:@“png”];//请求将图像保存到相机卷UIImageWriteToSavedPhotosAlbum(甘特图,nil,nil,nil);同样的漏洞在应用程序加载后立即出现。我在苹果开发者论坛上发布了这个问题,这里是链接。