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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:31:39+00:00 2026-06-16T22:31:39+00:00

I understand that __block in ARC retains the variable. This can then be used

  • 0

I understand that __block in ARC retains the variable. This can then be used when accessing a variable within a block before the variable has been assigned, as in:

  __block __weak id observer = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:player queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notif){
    // reference the observer here. observer also retains this block,
    // so we'd have a retain cycle unless we either nil out observer here OR
    // unless we use __weak in addition to __block. But what does the latter mean?
  }];

But I am having trouble parsing this. If __block causes the observer to be retained by the block, then what does it mean to effectively be both strong and weak? What is the __weak doing here?

  • 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-06-16T22:31:40+00:00Added an answer on June 16, 2026 at 10:31 pm

    __block means that the variable is like global, that survives the current frame stack, and accessible by the block that you will declare in the scope.

    __weak means that the variable doesn’t retain the pointed object, but if the object gets deallocated the __weak pointer will be set to nil.

    In your case observer catches the return value of addObserverForName:object:queue:usingBlock: , so it doesn’t need to be strong. And if was strong then it was retaining the observer, making it global and surviving until the strong reference was set to nil.

    Example

    #import <Foundation/Foundation.h>
    
    void (^foo()) ()
    {
        NSString* str= [[NSString alloc]initWithFormat: @"Hey"];
        __block __weak NSString* x= str;
        return ^
        {
            NSLog(@"%@",x);
        };
    }
    
    int main(int argc, char** argv)
    {
        @autoreleasepool
        {
            void (^block) ()= foo();
            block();
        }
        return 0;
    }
    

    This example prints (null), let’s see what happened:

    • The string @”Hey” has 1 as ARC reference count;
    • It is used in the block, but since it’s weak it’s not retained by the block, so it still has the count to 1;
    • The block is returned and executed, since we exit from the foo function scope, the string is deallocated and the pointer x is set to nil.

    So you could just do it like this:

    void (^foo()) ()
    {
        NSString* str= [[NSString alloc]initWithFormat: @"Hey"];
        return ^
        {
            NSLog(@"%@",str);
        };
    }
    

    There’s no problem: str is strong by default so it’s captured, you don’t need the __block specifier.

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

Sidebar

Related Questions

I understand that this question has been asked multiple times, however I need further
See this example: http://jsfiddle.net/EwtaC/1/ The images are styled via display:inline-block. I understand that whitespace
I understand that the synchronized keyword is used to prevent multiple threads from accessing
I understand that it is good practice to use a using block when getting
I understand that this question may be subjective, this is why I need an
I understand that applications under the same domain name can talk to each other
I understand that we can set the various style attributes from code behind using
In .NET when you make an async call I understand that this call is
From the recv(2) man page: MSG_WAITALL This flag requests that the operation block until
I understand that the new TPL (Task Parallel Library) has implemented the Parallel.ForEach such

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.