在iPhone中使用NSData的内存泄漏问题 [英] memory leak problem using NSData in iPhone

查看:637
本文介绍了在iPhone中使用NSData的内存泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内存泄漏问题 - NSConcreteData

  //设置提示 - 相框中的照片
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage * cellThumbImg;
if([data length]> 0){cellThumbImg = [UIImage imageWithData:data];} else {cellThumbImg = [UIImage imageNamed:@130X90.gif]; }
UIImageView * imgView = [[UIImageView alloc] initWithImage:cellThumbImg]; imgView.frame = photoFrame;
(cellThumbImg.size.height> = 58 || cellThumbImg.size.width> = 58)? [imgView setContentMode:UIViewContentModeScaleToFill]:[imgView setContentMode:UIViewContentModeCenter];
[cell.contentView addSubview:imgView];
[imgView release];

我的问题与这个问题非常相似,



iPhone - 内存泄漏 - NSData dataWithContentsOfUrl& UIWebView



即使,我已将以下代码添加到我的应用程序已完成启动,如下所示。
以下代码用于设置具有零容量的sharedCache内存。它将几乎删除NSConcreteData泄漏在我的应用程序。但是内存泄漏。

   - (void)applicationDidFinishLaunching:(UIApplication *)application {
NSURLCache * sharedCache = [[ NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
[window makeKeyAndVisible];
}

我无法从堆栈溢出中找到这种问题的解决方案。 / p>

如果您可以回答,我将非常感谢您。



提前感谢。

解决方案

我在大项目中也遇到了问题。在与一名苹果工程师合作寻找漏洞之后,他最后向苹果的主要开发团队提出了NSURLConnection的问题。他们基本上说,有一个内部缓存,在NSURLConnection中是不可以清除的,这是一个已知的问题。



所以我开始寻找替代品。我发现ASIHTTPConnection(链接下面),工作关闭CFNetwork。它被设计为NSURLConnection的一个替代,加上一堆其他真棒的好物,如下载到磁盘而不是内存,下载恢复,进度栏回调等。



我在所有的项目中使用它,从未有过任何问题或投诉。回答你的问题,这是我摆脱这些内存泄漏。



http://allseeing-i.com/ASIHTTPRequest/


Memory leak problem - NSConcreteData

// to set tip - photo in photo frame    
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;
if([data length]>0){ cellThumbImg=[UIImage imageWithData:data];} else { cellThumbImg=[UIImage imageNamed:@"130X90.gif"]; }
UIImageView *imgView=[[UIImageView alloc]initWithImage:cellThumbImg]; imgView.frame=photoFrame;
(cellThumbImg.size.height>=58 || cellThumbImg.size.width>=58 ) ? [imgView setContentMode:UIViewContentModeScaleToFill] : [imgView setContentMode:UIViewContentModeCenter] ;
[cell.contentView addSubview:imgView]; 
[imgView release]; 

my question is very much similar to this question,

iPhone - Memory Leak - NSData dataWithContentsOfUrl & UIWebView

Even, I have added following code to my Application Did Finished Launching, given below. Following code is for setting sharedCache memory with zero capacity. It will almost remove the NSConcreteData leak in my application. However memory leaks.

- (void)applicationDidFinishLaunching:(UIApplication *)application {       
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
[window makeKeyAndVisible];
}

i could not find any solution for this kind of question from stack overflow.

If you can answer, i will be thankful to you.

Thanks in advance.

解决方案

I had issues with this as well in my Large project. After working with an Apple engineer on trying to locate the leaks, he finally asked the main Apple dev team behind NSURLConnection. They basically said that there is an internal cache that is not clearable at all in NSURLConnection and it was a known issue.

So I set out looking for alternatives. I found ASIHTTPConnection (link below) which works off of CFNetwork. It is designed to be a drop-in replacement for NSURLConnection, plus a bunch of other awesome goodies like downloading to disk instead of memory, download resuming, progress bar callbacks etc..

I have used it in all my projects and have never had any issues or complaints. An, in answer to your question, this is how I got rid of those memory leaks.

http://allseeing-i.com/ASIHTTPRequest/

这篇关于在iPhone中使用NSData的内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆