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

  • Home
  • SEARCH
  • 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 7054145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:35:28+00:00 2026-05-28T03:35:28+00:00

Mike Ash has written this introduction to ARC where he introduces something like: __weak

  • 0

Mike Ash has written this introduction to ARC where he introduces something like:

__weak Foo *_weakFoo = [object foo];

Why would I want to do that for a local, temporary variable? __weak is a zeroing reference which will set the _weakFoo pointer automatically to nil as soon as the referenced object gets deallocated. Also, __weak is only available in iOS >= 5.

When would I run into trouble when I simply do this?:

Foo *_weakFoo = [object foo];

This is always expected to return an object or nil. My guess is this:

Foo *_weakFoo = [object foo];
[self doSomethingStupid]; // does something bad so foo gets deallocated
[_weakFoo doIt]; // CRASH! msg sent to deallocated instance 0x123456

One thing that still bugs me with ARC is: When does it know that I don’t need an object anymore? I’d argue that when I set a pointer to nil or to something else it figures out that the previously referenced object isn’t needed by this owner anymore and therefore maybe can go away. But the point is: I set it to nil. So it’s nil anyways!

So when would __weak for a local variable make sense, and what kind of crazy thing must I do somewhere else so that I really need that?

  • 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-28T03:35:28+00:00Added an answer on May 28, 2026 at 3:35 am

    I use __weak local variables if I have to manipulate self inside of a block to avoid a retain cycle. Consider this example where I’m using GCD and blocks to perform a network request for a string, and then setting it on a label declared by the class, in this case, TurtlesViewController.

    __weak TurtlesViewController *weakSelf = self;
    dispatch_queue_t networkQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(networkQueue, ^{
    
        // Kick off a network task for some data that is going to populate a label declared on our class
        NSString *returnString = [networkDataSource retrieveTurtleTime];
    
        // Dispatch back to the main thread to populate the UILabel
        dispatch_async(dispatch_get_main_queue(), ^{
    
            // Using self.label here creates a retain cycle. Self owns the block and the block has captured self
            self.label.text = returnString;
    
            // Instead, we use weakSelf for our reference to the label as it will be torn down by ARC at the end of the loop.
            weakSelf.label.text = returnString;
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there in VB.NET something like this in php?: $var[a1]['name']=Mike; $var[a1]['nick']=MMM; I tried hashtables,
BACKGROUND I'm creating an AJAX chat system. It looks something like this: Mike -
I came accross this on the Mike Ash Care and feeding of singletons and
I have a string like this: $a = Mike , Tree ; I want
Would it be possible to change Hello, this is Mike (example) to Hello, this
dear all, i have a string like this, ...1name: john, 2name: lice, 3name: mike....
this is what I have: SELECT `id`, `names` FROM `screenplays` WHERE `names` LIKE '%joe%mike%'
my table look like this.. id name count -- ---- ----- 1 Mike 0
Given a dictionary (or Counter) of tally data like the following: d={'dan':7, 'mike':2, 'john':3}
I'm new to scala. I tried this code: val name = mike println(name.getClass()) It's

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.