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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:47:10+00:00 2026-06-15T13:47:10+00:00

from researching memory management on objective C, I have a question about reference count:

  • 0

from researching memory management on objective C, I have a question about reference count:

I have an object name obj_number. I have alloc it

    obj_number = [[NSNumber alloc] init];

and then in another method (named A)of this class, I set

    obj_number = [dataset objectAtIndex:0];

go out this method A dataset will be autorelease. I checked this after go out this method obj_number also can’t be access, maybe it also be deallocate.

I see if an object just be under one ownership, so if parent deallocate it also be deallocate, but in my case I suppose that obj_number under 2 ownership (retain count may equals to 2), so why running out method A it be released?

  • 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-15T13:47:12+00:00Added an answer on June 15, 2026 at 1:47 pm

    You need to provide a setter method for obj_number, which you can do by providing a @property in the .h file and a @sythesize in the .m file. The setter method will ensure that the old value is released and the new value is retained.

    MyClass.h:

    @interface MyClass : NSObject
    {
        NSNumber *_obj_number;    // Use a different name for the ivar and property!
        ...
    }
    
    @property (retain, nonatomic, readwrite) NSNumber *obj_number;
    
    ...
    
    @end
    

    MyClass.m:

    @implementation MyObject
    @synthesize obj_number = _obj_number;
    
    ...
    
    - (void)dealloc
    {
        self.obj_number = nil;
        ...
        [super dealloc];
    }
    

    And when assigning to obj_number, you must relinquish ownership:

    - (void)someMethod
    {
        // We own myNumber, so release
        NSNumber *myNumber = [[NSNumber alloc] initWithUnsigned:12];
        self.obj_number = myNumber;
        [myNumber release];
    
        // However this is easier:
        self.obj_number = [NSNumber numberWithUnsigned:12];
    }
    
    - (void)someOtherMethod
    {
        // We don't own [dataset objectAtIndex:0] so no need to release
        self.obj_number = [dataset objectAtIndex:0];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been researching this for a while but got no convinced answer. From
I noticed an interesting result from JSLint while researching a codereview question. JSLint complained
I have been researching switching my allocation method from simpling overloading new to using
I have been researching how best to retrieve subsets of location data from a
I have been searching this since yesterday and still nothing. From all that researching
I have just started researching creating mobile apps and from what I understand one
I've been researching a bit on how to make JNI calls from C++ to
From my website, I have a button which calls a method and then generates
From 2.13.2/3 The double quote and the question mark ? , can be represented
I was researching on the singleton pattern for C# I found this example from

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.