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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:01:54+00:00 2026-06-11T14:01:54+00:00

Let’s say my class looks like @interface MyClass { MyObject* _object; dispatch_queue_t _queue; }

  • 0

Let’s say my class looks like

@interface MyClass {
    MyObject* _object;
    dispatch_queue_t _queue;
}
-(void)myBlocksUsingMethod;
@end

Ignoring the semantics of initializing the queue, now I implement

-(void)myBlockUsingMethod {
    dispatch_async(_queue, ^{
        [_object doSomething];
    });
}

As the above code stands, is retaining self in the block ok?

I could rewrite the block like

-(void)myBlockUsingMethod {
    __weak MyClass* weakSelf = self;
    dispatch_async(_queue, ^{
        MyClass* strongSelf = weakSelf;
        [strongSelf._object doSomething];
    });
}

But now is it necessary to test if strongSelf == nil in this situation given that my _queue is an iVar of the object I’m retaining?

Also, what happens if my doSomething method were to push another block that references self onto the same queue? Will that cause a retain cycle?

  • 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-11T14:01:59+00:00Added an answer on June 11, 2026 at 2:01 pm

    To answer the question in the title: Yes, there are many reasons to do this. You may know for certain that the object will outlive the block, or you may want to create a retain cycle that will be broken manually at a specific time later.

    However, in your code, you are doing something quite wrong. The first method (the one that creates the implicit strong reference to self) should generate a warning.

    The second one should not compile. You have this:

    [strongSelf._object doSomething];
    

    which is invoking a property named _object and I doubt that’s the case. So, maybe you meant this:

    [strongSelf.object doSomething];
    

    in which case, you will be fine because if strongSelf is nil then a message sent to nil effectively does nothing.

    Or, if there is no property, maybe you really mean this:

    [strongSelf->_object doSomething];
    

    in which case, your program will blow up if strongSelf is nil, because this is not sending a message to nil but dereferencing a null pointer. If you do the latter, you need to check for nil explicitly.

    Since you do not access _queue in the block, that access is fine. Any direct access of an iVar inside a block must either create a retain cycle to make sure the object is still alive, or it must play the weak-strong-dance and check for nil.

    If you use the object in multiple lines, you also need to do the weak-strong-dance because the weak object could dealloc in between accesses (the runtime guarantees that if a __weak is not nil, and accepts a message, that it will stay alive until that message has been handled).

    [weakObject message1];
    // weakObject can become nil here
    [weakObject message2];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have a set of relations that looks like this: relations
let's say we have some simple code like this : private static void Main()
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say there is a graph and some set of functions like: create-node ::

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.