Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:01:16+00:00 2026-05-27T07:01:16+00:00

Mike Ash created an example of using blocks to handle callbacks from sheets, which

  • 0

Mike Ash created an example of using blocks to handle callbacks from sheets, which seems very nice. This was in turn updated to work with garbage collection by user Enchilada in another SO question at beginSheet: block alternative?, see below.

@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(block)];
}

- (void)my_blockSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
  void (^block)(NSInteger returnCode) = contextInfo;
  block(returnCode);
  Block_release(block);
}

@end

While enabling GC, this does not work with Automatic Reference Counting (ARC). Myself, being a beginner at both ARC and blocks, can’t get it to work. How should I modify the code to get it to work with ARC?

I get that the Block_release() stuff needs to go, but I can’t get past the compile errors about casting ‘void *’ to ‘void (^)(NSInteger)’ being disallowed with ARC.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:01:17+00:00Added an answer on May 27, 2026 at 7:01 am

    ARC doesn’t like conversions to void *, which is what the Block_* functions expect as arguments, because ARC cannot reason about ownership of non-retainable types. You need to use bridge casts to tell ARC how it should manage the ownership of the objects involved, or that it shouldn’t manage their ownership at all.

    You can solve the ARC issues by using the code below:

    - (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((__bridge void *)block)];
    }
    
    
    - (void)my_blockSheetDidEnd:(NSWindow *)sheet
                     returnCode:(NSInteger)returnCode
                    contextInfo:(void *)contextInfo
    {
        void (^block)(NSInteger) = (__bridge_transfer id)contextInfo;
        block(returnCode);
    }
    

    In the first method,

    Block_copy((__bridge void *)block)
    

    means the following: cast block to void * using a __bridge cast. This cast tells ARC that it shouldn’t manage the ownership of the operand, so ARC won’t touch block memory-management-wise. On the other hand, Block_copy() does copy the block so you need to balance that copy with a release later on.

    In the second method,

    void (^block)(NSInteger) = (__bridge_transfer id)contextInfo;
    

    means the following: cast contextInfo to id (the generic object type in Objective-C) with a __bridge_transfer cast. This cast tells ARC that it should release contextInfo. Since the block variable is __strong (the default qualifier), the Block is retained and, at the end of the method, it is finally released. The end result is that block gets released at the end of the method, which is the expected behaviour.


    Alternatively, you could compile that category with -fno-objc-arc. Xcode allows files in the same project to build with or without ARC enabled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a basic site using ASP.NET routing according to Mike Ormond's example
I came accross this on the Mike Ash Care and feeding of singletons and
For example, I have a table which looks like this : id | name
Mike Lischke's TThemeServices subclasses Application.Handle , so that it can receive broadcast notifications from
Would it be possible to change Hello, this is Mike (example) to Hello, this
I am using a jQuery Multi-Friend Selector form this site; http://mike.brevoort.com/2010/08/10/introducing-the-jquery-facebook-multi-friend-selector-plugin/ And it works
Take this enum for example: public enum PersonName : short { Mike = 1,
I'm using fnh and castle nhib facility. I followed the advice from mike hadlow
I got this string: from:100000238267321=to:100000238267322=somethingelse: hey james, heard the news, mike is returning tomorrow!
Mike Ash has written this introduction to ARC where he introduces something like: __weak

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.