Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

ios - iPhone - Memory Leak - NSData dataWithContentsOfUrl & UIWebView

Please help! Have been staring at this for 12 hours; and have looked online and can't find solution.

In my application, I use 2 UIView controls in separate pages/controllers:

  • UIImageView (retrieve data via NSData dataWithContentsOfUrl)
  • UIWebView

Just to isolate my code, and make it easier to explain, I created a new view based project called "MyTestApplication"

1 - I added a simple NSData dataWithContentsOfUrl in the delegate function.

NSData *imageData = [NSData dataWithContentsOfURL:
  [NSURL URLWithString:@"http://www.google.com/intl/en_ALL/images/logo.gif"]];

(Nothing to release here since it's all using convenience functions)

alt text http://img.skitch.com/20081110-j5tn5n7ixph95ys4rpchibaw5p.preview.jpg

View Image

2 - Run it to verify no leaks (as expected)

alt text http://img.skitch.com/20081110-fy2qrkgy47hm4fe2f1aakd4muw.preview.jpg

View Image

3 - Open the ViewController.xib and simply add a UIWebView from the library (no need to wire it up)

alt text http://img.skitch.com/20081110-d63c3yh1a1kqiciy73q8uyd68j.preview.jpg

View Image

4 - Run it to verify there are leaks! (why???)

alt text http://img.skitch.com/20081110-qtxcfwntbcc3csabda3r6nfjg6.preview.jpg

View Image

What am I doing wrong? Please help!

Why would NSData cause memory leak if I'm using UIWebView? I just don't get it. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I was also having trouble with leaks from NSData's dataWithContentsOfURL: in the iPhone simulator. I found that when I used the other convenience method (dataWithContentsOfURL:options:error:)?I would not get the memory leak.

My code looked something like this:

NSURL *url = [NSURL URLWithString:urlString];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url
                                     options:0
                                       error:&error];

Link to documentation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...