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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:32:26+00:00 2026-06-02T05:32:26+00:00

By books first explains the problem (using ARC) about unvalid references, like in this

  • 0

By books first explains the problem (using ARC) about “unvalid” references, like in this example:

NSDate* date1=[[NSDate alloc]init];
NSDate* date2=date1;
[date1 release];
NSLog(@"%@",date2); // bad access

So I have understood the retain/release mechanism: in this case the instruction would be:

date2=[date1 retain];

But when it talks about strong/weak references, it sounds like a contradiction to me:

“By default, references are strong. If you assign an object to a strong reference, ARC assumes that you want that object to stick around and retains it implicitly”

Isn’t this a contradiction of what has said before?
date2 is strong by default, so it should implicitly retain date1 and there wouldn’t be a bad access exception.
Of course I have misunderstood something, could someone explain this better to me?

  • 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-02T05:32:28+00:00Added an answer on June 2, 2026 at 5:32 am

    Strong is the default because it’s typically what you want, but using ARC, the compiler analyzes how long the object’s life needs to be and releases the memory at the appropriate time. For example:

    - (void)someMethod
    {
      NSDate* date = [[NSDate alloc] init]; // date is __strong by default
      NSLog(@"The date: %@", date); // date still contains the object created above
    
      // Sometime before this point, the object date pointed to is released by the compiler
    }
    

    Weak references only keep the object around while it has one or more other strong references to it. As soon as the last strong reference is broken, the object is released by the compiler and the weak object reference (the variable) is changed to nil by the runtime. This makes weak variables almost useless in the local scope like the example above. For example:

    - (void)someMethod
    {
      __weak NSDate* date = [[NSDate alloc] init]; // The date created is released before it's ever assigned to date 
                                                   // because date is __weak and the newly created date has no 
                                                   // other __strong references
      NSLog(@"The date: %@", date); // This always prints (null) since date is __weak
    }
    

    To see an example of a weak and strong variable working together in the local scope (this would have only severely limited usefulness and is really shown here only to demonstrate weak variable references):

    - (void)someMethod
    {
      NSDate* date = [[NSDate alloc] init]; // date stays around because it's __strong
      __weak NSDate* weakDate = date;
    
      // Here, the dates will be the same, the second pointer (the object) will be the same
      // and will remain retained, and the first pointer (the object reference) will be different
      NSLog(@"Date(%p/%p): %@", &date, date, date);
      NSLog(@"Weak Date(%p/%p): %@", &weakDate, weakDate, weakDate);
    
      // This breaks the strong link to the created object and the compiler will now
      // free the memory. This will also make the runtime zero-out the weak variable
      date = nil;
    
      NSLog(@"Date: %@", date); // prints (null) as expected
      NSLog(@"Weak Date: %@", weakDate); // also prints (null) since it was weak and there were no more strong references to the original object
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m a little confused about this point. Everything that I found in books, blogs,
I'm almost finished with the book Head First Java. The reason I'm studying this
After reading the Head First Design Patterns book and using a number of other
I just read about the breadth-first search algorithm in the Introduction to Algorithms book
I read below line in one book about buffered input Why have buffers? First,
After reading about at least the first 3 or 4 chapters of about 4
A book as the following example and explains that you won't see the line
This is clearly not appears like it wouldn't be a best practice. Can someone
When I first started learning about Spring, things were configured in the applicationContext.xml file.
All the online references and head first JSP&Servlet book I'm reading state the characteristic

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.