Re: Memory Leaks and ARC
Re: Memory Leaks and ARC
- Subject: Re: Memory Leaks and ARC
- From: Dave <email@hidden>
- Date: Wed, 23 Apr 2014 11:01:29 +0100
On 23 Apr 2014, at 00:18, Quincey Morris <email@hidden> wrote:
> On Apr 22, 2014, at 15:18 , Dave <email@hidden> wrote:
>
>> I assumed that ARC would release myImage on each interation of the loop, however, this seems not to be the case
>
> The ‘myImage’ variable gives up *ownership* of the previous image object when you create a new one in the loop, but whether that leads to a deallocation of the object is an implementation detail. If it’s autoreleased anywhere in its [short] lifetime, the image object will linger in the autorelease pool until the pool is drained, as Keary said.
>
> If you want to avoid autoreleases, you should ensure that the image is returned from the download method with +1 ownership semantics. As far as that method is concerned, you can do this by changing its name to start with ‘new…’, or decorate its declaration with ‘__attribute__((ns_returns_retained))’. You would, of course, have to ensure that the download method and the methods it calls don’t do anything else that’s likely to retain/autorelease the image.
>
Ahhhh, I didn’t realize that ARC follows the same rules, e.g. new, alloc, in method names.
The flow of control goes as follows:
test calls:
self commandDownloadImageWithFileURLString calls:
self.pServerAPI newDownloadImageFileWithURL: <———— Already returns a +1 “owned” Object
> This sort of thing is a bit messy because it relies on presumed (but partially documented) knowledge of the ARC implementation. It’s more robust and straightforward to do what Keary says. However, if you really wanted to avoid autoreleases completely, there are ARC strategies that can help.
>
If I changed the names of commandDownloadImageWithFileURLString to be newCommandDownloadImageWithFileURLString, this would cause it to release myImage on each iteration rather than using Autorelease?
Is there anyway of telling if an Object is in “autorelease” state? I mean just for testing, I wouldn’t rely on this in shipping code.
I’d rather avoid autorelease if possible, it has only ever caused me grief in the long run!
Cheers
Dave
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden