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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:24:48+00:00 2026-06-09T10:24:48+00:00

In this question , I asked about the following code and retain cycles: __weak

  • 0

In this question, I asked about the following code and retain cycles:

__weak Cell *weakSelf = self;
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        UIImage *image = /* render some image */
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [weakSelf setImageViewImage:image];
        }];
    }];
    [self.renderQueue addOperation:op];

All answers state that using a weak reference here was not necessary, since this code does not result in a retain cycle. However, while experimenting with some more code, the following does result in a retain cycle (if I don’t use a weak reference, the current view controller is not deallocated)

    //__weak ViewController *weakSelf = self;
    MBItem *close = [[MBItem alloc] initWithBlock:^{
        [self dismissModalWithDefaultAnimation:NO];
    }];
    NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:close, nil];
    [self.childObject setItems:items];

Why would the second one result in a retain cycle but not the first one?

  • 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-09T10:24:49+00:00Added an answer on June 9, 2026 at 10:24 am

    Your old code creates this retain cycle if you don’t use __weak:

    • (NSBlockOperation *)op retains the outer block
    • The outer block retains self (if you’re not using __weak)
    • self retains (NSOperationQueue *)renderQueue
    • (NSOperationQueue *)renderQueue retains (NSBlockOperation *)op

    None of the objects in that cycle can be deallocated unless one of those links is broken. But the code you showed us does break the retain cycle. When op finishes executing, renderQueue releases it, breaking the retain cycle.

    I suspect that your new code creates this retain cycle:

    • (MBItem *)close retains the block
    • The block retains self
    • self retains childObject
    • childObject retains (NSMutableArray *)items
    • (NSMutableArray *)items retains (MBItem *)close

    If nothing happens to break one of those links, none of the objects in the cycle can be deallocated. You haven’t shown us any code that breaks the retain cycle. If there is no event that explicitly breaks it (for example by clearing out childObject.items), then you need to use __weak to break the retain cycle.

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

Sidebar

Related Questions

This is related to this question I asked earlier about syntax highlighting user-defined blocks
Note: I originally asked this question about an hour ago but only recently realized
This stems from a previous question I asked - about a write conflict with
This is a follow up question to a previous question I asked about calculating
I asked a question about different testing frameworks yesterday. This question can be found
A few days ago I asked this question about jquery ajax function invoking action
I have tried looking though several of the already asked question about this topic
This question has been asked in a C++ context but I'm curious about Java.
Hi i was asked in an interview about this question. I did google a
Yesterday I asked this general question about decimals and their internal precisions. Here is

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.