• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: autorelease: how does this work!? (if at all)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: autorelease: how does this work!? (if at all)


  • Subject: Re: autorelease: how does this work!? (if at all)
  • From: Jeff Kelley <email@hidden>
  • Date: Fri, 18 Jun 2010 11:11:06 -0400

Blocks do retain objects that are passed in, so that's probably what's happening. Also, keep in mind that autoreleased objects aren't freed immediately; it happens when the run loop completes. You should always act as if it does happen immediately, but it isn't always true.

Jeff Kelley

On Jun 18, 2010, at 11:00 AM, Jonny Taylor wrote:

> I've just been looking back at some code that has been working fine for me for a while, and as far as I can see it shouldn't actually work! I'd be interested for peoples' comments. The code is as follows:
>
> dispatch_async(queue1,
> ^{
> 	NSImage *theImage = [frame GetNSImage];
> 	NSData *tiffRep = [theImage TIFFRepresentation];
> 	dispatch_async(queue2,
> 	^{
> 		[tiffRep writeToFile:[NSString stringWithFormat:@"%@%d.tif",
> 					[[frame Camera] ExportFilePrefix], [frame FrameNumber]]
> 				atomically:YES];
> 	});
> 	[theImage release];
> });
>
> Work running on serial queue "queue1" calculates a TIFF representation for an image, and then schedules work on serial queue "queue2" to write that data to disk. What I can't work out is why tiffrep isn't autoreleased as soon as the outer block completes. Is the compiler/runtime being clever enough to retain it because it is going to be needed in the inner block (if so: very clever!)? If not, am I just getting lucky here with exactly when/how grand central does its autorelease cleanup? Or maybe the TIFF representation and/or my frame data is still being retained elsewhere for a while (possible, depending on how the thread timings work out...).
>
> I'm pretty new to Cocoa so I'm keen to understand this properly - I'd be interested to hear peoples thoughts on this: is what I am doing ok, or do I need to add some explicit retain/releasing of tiffRep?
>
> Cheers
> Jonny
_______________________________________________

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

References: 
 >autorelease: how does this work!? (if at all) (From: Jonny Taylor <email@hidden>)

  • Prev by Date: Re: Base class/subclass model in objective c
  • Next by Date: Re: autorelease: how does this work!? (if at all)
  • Previous by thread: autorelease: how does this work!? (if at all)
  • Next by thread: Re: autorelease: how does this work!? (if at all)
  • Index(es):
    • Date
    • Thread