开发者

iphone UIImage memory leaks

开发者 https://www.devze.com 2023-01-28 14:22 出处:网络
I\'m implementing an image browser, using a UIScrollView. Due to memory costranints, I\'ve to implement image dynamic loading (I don\'t want use CATiled Layers because it forces user remaining waiting

I'm implementing an image browser, using a UIScrollView. Due to memory costranints, I've to implement image dynamic loading (I don't want use CATiled Layers because it forces user remaining waiting to load every tile).

I've tried in a coupe of ways:

- (UIImageView*) ldPageView{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
NSError *error;
NSData *imData = [NSData dataWithContentsOfURL:ldPageRef options:NSDataReadingUncached error:&error];
UIImage *im = [[UIImage alloc] initWithData:imData];
ldView = [[UIImageView alloc] initWithImage:im] ;
[ldView setFrame:pageRect];
[pool release];  // Release the objects in the pool.
return ldView;
}

And even in this way

- (UIImageView*) ldPageView{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
CGDataProviderRef provider = CGDataProviderCreateWithURL ((CFURLRef)ldPageRef);
CGImageRef d = CGImageCreateWithJPEGDataProvider(provider,nil, true,kCGRenderingIntentDefault);
UIImage *im = [[UIImage alloc] initWithCGImage:d];
ldView = [[[UIImageView alloc] initWithImage:im] autorelease];
[im release];
CGDataProviderRelease(provider);
CGImageRelease(d);
[ldView setFrame:pageRect];
[pool release];  // Release the objects in the pool.
return ldView;
}

But every time I try it both on simulator and on iPad, memory explodes. I've runned my code with Instruments and no leak is reported. ldView is an istance variable and it is deallocated togheter with ldPageRef on object dealloc (which is called for sure).

I've also tried setting NSURLCache sharedCache to nil or to zero, but it is still happening.

I've read Memory management guide, but every开发者_StackOverflowthimg seems ok to me. Please help


Try using

UIImage *im = [UIImage imageWithData:imData];

rather than

UIImage *im = [[UIImage alloc] initWithData:imData];

Always avoid allocs if possible otherwise you must ensure that you manually release the object.


More than likely it is how you are creating your UIImage. Try creating your image as such..

[UIImage imageWithData:imData];

instead of

[[UIImage alloc] initWithData:imData];  

This will return a autoreleased object(it is a class method) so that you will not have to try to release it yourself later.


You are never releasing your alloc'd objects. You need to change:

[[UIImage alloc] initWithData:imData];
[[[UIImageView alloc] initWithImage:im];

to:

[[[UIImage alloc] initWithData:imData] autorelease];
[[[UIImageView alloc] initWithImage:im] autorelease] ;


Indeed I have found a memory leak in UIImageView. You just never pay any attention to it, since you may open images from the App package all the time, and these images are being cached by iOS.

But if you download a number of images from the network (say 40 iPhone-camera photos), save them to the documents and open them over and over again in a sub view controller, the memory leak applies. You do not need to have 40 different images, it is enough to load one image again and again.

Your test app is with ARC disabled and an image is being loaded from file and displayed in a sub view controller, every time that controller is being pushed.

In your sub view controller you'll create an UIImageView and assign the UIImage object to the image view's .image property. When you leave the sub view controller, you release the image view correctly. On an iPhone 4s your app won't open more than 80 images. Mine crashed at around 70 images (with iOS 6.1). And have a look on the intruments app, before the crash. The memory is full of CFMalloc blocks.

The solution I found is simple: Set the image property to nil, before you release the image view. Again, look at the instruments app. It now looks like what you'd expect and your app does no longer crash.

I think the same leak applies to whatever the UIWebView uses to display images and Apple doesn't know about it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号