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 7539199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:19:11+00:00 2026-05-30T07:19:11+00:00

Is it ok to access a delegate property from an Block? @interface TheObject :

  • 0

Is it ok to access a delegate property from an Block?

@interface TheObject : NSObject
...
@property (nonatomic, assign) id<SomeDelegate> delegate;

@synthesize delegate

- (void) someMethod {
  [someObject doSomethingWithCompletionHandler:^(NSArray *)someArray {
      [self.delegate otherMethod:someArray];   
 }];
}

What happens if the delegate is nilled (from the dealloc method in the object that has also set the delegate) before the completion handler is called?
Could it be a memory bug?
I don’t know how to use __block for properties…

Answer from below:

If the delegate is nilled from the object which is the delegate on the dealloc call, everything is fine.

@property (nonatomic, retain) TheObject theObject;

@synthezise theObject = _theObject;

- (void) thatMethod {
  self.theObject = [[TheObject alloc] init] autorelease];
  _theObject.delegate = self;
}

- (void) dealloc {
  _theObject.delegate = nil;
  self.theObject = nil;
}
  • 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-30T07:19:13+00:00Added an answer on May 30, 2026 at 7:19 am

    Normally, if your delegate is deallocated before the block is executed, then it would access garbage, since the block is an assign property and the block retains self rather than the delegate since you access it by reference.

    However, since you’ve set it up so that self.delegate gets nilled if delegate is deallocated, you won’t have that problem. Instead, if your delegate were deallocated, then in your code you’d just be sending the otherMethod: method to nil, which would do nothing, but also cause no errors.

    If you want the method to definitely be sent to your delegate, the solution is to access it by value instead of reference:

    - (void)someMethod {
        id <SomeDelegate> delegateForBlock = self.delegate;
        [someObject doSomethingWithCompletionHandler:^(NSArray *)someArray {
            [delegateForBlock otherMethod:someArray];   
        }];
    }
    

    That way delegateForBlock will be a pointer to the same object as self.delegate (at the time you execute someMethod:), and it will be retained.

    To find out more about how this works, check out Blocks Programming Topics.

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

Sidebar

Related Questions

Question: I'm trying to access a variable from the UITableView tableView:didSelectRowAtIndexPath: delegate method. The
How to access a property value of AppDelegate class from someView Controller without creating
I'm still new to delegates and I've been playing with the Delegate-based Data Access
I declared a NSMutableArray in the header-file with: NSMutableArray *myMuArr; and @property (nonatomic, retain)
I found several posts online stating that I could access my application delegate object
I'm trying to create delegates to access any Property Get & Set methods. I
It's nothing new to de-couple the data access code from your business objects, but
Good Day. On my AppDelegate.h @property (nonatomic, retain) Config *AppConfig; Within my AppDelegate.m, I
In my app, I have: car.h @interface car : NSObject { NSString *model; NSString
To retrieve data from ActiveDirectory I used the following code: SPSecurity.RunWithElevatedPrivileges(delegate() ( var currentDirectory

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.