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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:01:30+00:00 2026-05-11T18:01:30+00:00

I was reading the apple documentation for memory management , and came across something

  • 0

I was reading the apple documentation for memory management, and came across something that I just don’t understand. Basically, I don’t understand why one does not need need to retain an instance variable through the "getter" method. I wrote this little program to see what would happen. I thought there would be a crash, but I am obviously missing something.

//  main.m
//  Test
//


#import <Foundation/Foundation.h>
#import "Test.h"

int main(int argc, char *argv[])
{
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
    
    //Initialize the test object
    Test *t = [[Test alloc] init];
    
    //Set the value to 5
    [t setMyNum:[NSNumber numberWithInt:5]];
    
    //Save a temp number that points to the original number
    NSNumber *tempNum = [t myNum];
    
    //release old number and retain new
    [t setMyNum:[NSNumber numberWithInt:7]];
    
    //Shouldn't this crash because tempNum is pointing to a deallocated NSNumber???
    NSLog(@"the number is %@",tempNum);
    
    [p drain];
    return 0;
}

Doesn’t tempNum point to a deallocated object??

All help is appreciated.

EDIT

This is the code in the getter and setter methods

#import "Test.h"


@implementation Test
- (void)setMyNum:(NSNumber *)newNum {
    [newNum retain];
    [myNum release];
    myNum = newNum;
}

-(NSNumber *)myNum {
    return myNum;
}
@end

As you can see I am calling release on the old object.

EDIT

It was suggested, and I thought rightfully so that the reason the tempNum is still around is because it hadn’t been autoreleased from the pool yet. But even after moving the [pool drain] to right before the NSLog message, there is not crash??? Weird.

  • 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-11T18:01:30+00:00Added an answer on May 11, 2026 at 6:01 pm

    Since you are not explicitly releasing any objects, nothing is being deallocated until the autorelease pool is allowed to drain. Try inserting [p drain] before the last NSLog call. It should crash the NSLog call.

    Additionally, if you are not retaining the NSNumber in your setMyNum: method, you will find that it will crash if you add [p drain] before tempNum is assigned.

    To clarify the original question, calling a getter method doesn’t (and shouldn’t) necessarily imply that the caller wants to take ownership (i.e. retain) the variable. If that was the case, this code would leak:

    NSLog("Number is %@", [t myNum]);
    

    Also, it appears that NSNumber has an optimization whereby for small numbers, they cache the NSNumber objects, retain an extra copy, and return that version. So for small constants, [NSNumber numberWithInt: N] will return an object with 2 reference counts (available via [theNumber retainCount]). To explicitly see what happens, use a larger constant in the program, an NSNumber will retain a ‘fresh’ object with a reference count of 1 (that will also be autoreleased).

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

Sidebar

Related Questions

I've been reading an apple document on Memory Management and am now a bit
I'm reading Apple's documentation on using QTKit to capture streaming audio and video from
I've been reading through the adobe pdf spec, along with apple's quartz 2d documentation
I'm creating a UIView similar to Apple's Scrolling example and I have a question
I've currently got a spreadsheet type program that keeps its data in an ArrayList
I've been reading about how to support the zooming for a webview, and i've
I had one of my colleagues come to me today with a problem of
I am attempting to create a 3 tab iPhone Application with UITableViews on 2
Sorry if this sounds like a bit of a noobie question, i'm still relatively
Assuming this is possible, I would like my iOS application, when backgrounded, to poll

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.