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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:39:09+00:00 2026-05-29T06:39:09+00:00

I have the following simple code for an object that holds a weak reference:

  • 0

I have the following simple code for an object that holds a weak reference:

// interface

@interface GMWeakRefObj : NSObject
@property (weak) id object;
@end

// implementation

@implementation GMWeakRefObj 
@synthesize object;
@end

When I run the following test code it fails on the second assert:

NSData* d = [[NSData alloc] init];
GMWeakRefObj* weakRef = [[GMWeakRefObj alloc] init];
weakRef.object = d;
NSAssert(weakRef.object != nil, @"Reference wasn't assigned");
d = nil;
NSAssert(weakRef.object == nil, @"Reference wasn't zeroed"); // <-- FAIL

Aren’t ARC weak references supposed to be zeroing? And if so what am I doing wrong?

  • 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-29T06:39:10+00:00Added an answer on May 29, 2026 at 6:39 am

    Try some custom class class instead of NSData for d, e.g. MyData. Implement dealloc method in it and set breakpoint in it. You will see, that dealloc is called by autorelease pool after the last NSAssert. Only after that week reference will become nil.

    ADD:
    Looks like I have to extend my answer to make it clear, why it works that way.
    First, lets look at your example (from comments):

    NSData* data = [[NSData alloc] init];
    __weak NSData* weakRef = data;
    data = nil;
    NSAssert(weakRef == nil, @"Failed to zero");
    

    It works as expected, weakRef become nil after data = nil. The next example works too:

    NSData* data = [[NSData alloc] init];
    __weak NSData* weakRef = data;
    NSLog(@"%@", data);
    data = nil;
    NSAssert(weakRef == nil, @"Failed to zero");
    

    But the last example doesn’t work:

    NSData* data = [[NSData alloc] init];
    __weak NSData* weakRef = data;
    NSLog(@"%@", weakRef);
    data = nil;
    NSAssert(weakRef == nil, @"Failed to zero");
    

    The only difference is that we use weak reference to output log. Why?

    (the rest of the answer can be wrong 🙂 )

    Imaging that you NSLog (or any other function/selector we call before data = nil) rely on it’s argument not to be nil. For example, it has “if (arg == nil) return;” at the very beginning.

    In multithreaded environment weak reference can become nil after if.

    So properly written function should look like:

      // ...
      T* local_arg = arg;   // NOTE: it is strong!
      if (local_arg == nil)
        return;
      // work with local_arg here, not with arg
      // ...
    

    But usually we don’t want to do it everywhere — it will be ugly. So we want to be sure that arguments will not disappear somewhere in the middle. Compiler does it for us by autoreleasing weak reference.

    So, it should be clear how, why your GMWeakRefObj test case doesn’t work — weakRef is autoreleased before calling setObject setter.

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

Sidebar

Related Questions

I have the following code that fills dataTable1 and dataTable2 with two simple SQL
I have a simple 3D cube that I can rotate using the following code:
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have got the following very simple code: function init() { var articleTabs =
I have the following code (correct for my simple tests) for a linked list
I have a simple DOM code like the following <div> <div> </div> </div> I
I have used following code to create a simple PDF file. It executes fine
I have the following PHP code doing a very simple select into a table.
I have a simple application with the following code: FileInfo[] files = (new DirectoryInfo(initialDirectory)).GetFiles();

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.