2010/2/6 Frédéric Testuz <[email protected]>:
> Hi all,
>
> I'm beginning a new application targeting Snow Leopard only. So I want to use 
> the new tools (blocks) and garbage collection (GC). It will be my first 
> application using GC.
>
> I read some sites on GC and blocks. In the blog of Mike Ash I found some very 
> interesting propositions. My question concerns his code for using block with 
> sheets 
> (http://www.mikeash.com/?page=pyblog/friday-qa-2009-08-14-practical-blocks.html).
>  Is this code works in GC ?
>
> @implementation NSApplication (SheetAdditions)
>
> - (void)beginSheet: (NSWindow *)sheet modalForWindow:(NSWindow *)docWindow 
> didEndBlock: (void (^)(NSInteger returnCode))block
> {
>
> [self beginSheet: sheet
>        modalForWindow: docWindow
>        modalDelegate: self
>        didEndSelector: @selector(my_blockSheetDidEnd:returnCode:contextInfo:)
>        contextInfo: [block copy]];
> }
>
> - (void)my_blockSheetDidEnd: (NSWindow *)sheet returnCode: 
> (NSInteger)returnCode contextInfo: (void *)contextInfo
> {
> void (^block)(NSInteger returnCode) = contextInfo;
> block(returnCode);
> [block release];
> }
>
> @end
>
> I think, I have to replace [block copy] by CFRetain([block copy])
> and [block release] by CFRelease(block).

I believe your GC modifications are correct. As you've presumably
determined already, [block copy] alone isn't enough to keep the block
alive through the void *.

If you happen to be writing dual-mode code, you can write code that
works in both GC and refcounted environments by writing
CFRetain([block copy]) in the first part and CFRelease(block); [block
release]; in the second.

Mike
_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to