user2128531
user2128531

Reputation: 153

Arc memory leaks and UIImage named ios

Hi I have basically two questions

1) Is there still need of handling memory leaks while working on ios6 xcode 4.2 ARC environment as I came to know that arc only place a call at end of scope.

2) Statement

self.profileImageView.image = [UIImage imageNamed:@"createProfile_addPhoto.png"];

it place image in memory which is heavy process for screen. I read it at many places but not found any substitute code to replace this.Is there any better solution.

simply i need to handle memory problems.

Upvotes: 1

Views: 3715

Answers (4)

Reinhard Männer
Reinhard Männer

Reputation: 15217

To question 1):
Another possibility to have memory leaks with ARC is using a separate thread without an autorelease pool set up. Apple docs https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html:
"You must create your own autorelease pool block as soon as the thread begins executing; otherwise, your application will leak objects."
If no autorelease pool is set up, even calling a simple getter method will create memory leaks, if it returns an autorelease object.

Upvotes: 2

Andrea
Andrea

Reputation: 26385

I don't know if I understood well your question but:
1-ARC do manage all the memory for Objective-C object and in iOS6 also GCD queues
2-The method that you are using caches the image in memory for further calls. So If you are calling this image a lot of times is worth it(except if the image is really big).

If you need to create a smaller scope you can embed your code in an @autorelease block, useful when dealing with cycles, but if you use -imageNamed: as I told will cache the image. better use -imageWithContentsOfFile:.
Leaks in ARC are almost impossible, they happens if you are dealing with Core Foundation opaque type, that are not automatically managed by ARC. What could happen using ARC is abandoned memory or retain cycles, never heard about leaks using it.
Hope this helps, andrea

Upvotes: 2

Anil Varghese
Anil Varghese

Reputation: 42977

Instead of imageNamed use imageWithContentsOfFile:

 NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"createProfile_addPhoto" ofType:@"png"];

 self.profileImageView.image =  [UIImage imageWithContentsOfFile:imagePath];

Upvotes: 4

borrrden
borrrden

Reputation: 33421

1) Yes, for certain cases (i.e. strong reference cycles)

2) [UIImage imageWithContentsOfFile:] can be your substitute. imageNamed: implements a caching system. However, it will purge itself when memory gets tight.

Upvotes: 2

Related Questions