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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:12:35+00:00 2026-05-26T22:12:35+00:00

I need to know if the retaincount of objects should always be 0 if

  • 0

I need to know if the retaincount of objects should always be 0 if i want to have a good memory management in my code. I got the following code from a book. and there’s a statement NSLog called after release = 2, So should i have to release it 2 more times so that the retaincount will be 0 ?

#import <Foundation/NSObject.h>
    #import <Foundation/NSAutoreleasePool.h>
   #import <Foundation/NSString.h>
 #import <Foundation/NSArray.h>
 #import <Foundation/NSValue.h>
 int main (int argc, char *argv[])
 {
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 NSNumber *myInt = [NSNumber numberWithInteger: 100];
 NSNumber *myInt2;
 NSMutableArray *myArr = [NSMutableArray array];
 NSLog (@”myInt retain count = %lx”,
 (unsigned long) [myInt retainCount]);
 [myArr addObject: myInt];
 NSLog (@”after adding to array = %lx”,
 (unsigned long) [myInt retainCount]);
 myInt2 = myInt;
 NSLog (@”after asssignment to myInt2 = %lx”,
 (unsigned long) [myInt retainCount]);
 [myInt retain];
 NSLog (@”myInt after retain = %lx”,
 (unsigned long) [myInt retainCount]);


 NSLog (@”myInt2 after retain = %lx”,
 (unsigned long) [myInt2 retainCount]);
 [myInt release];
 NSLog (@”after release = %lx”,
 (unsigned long) [myInt retainCount]);
 [myArr removeObjectAtIndex: 0];
 NSLog (@”after removal from array = %lx”,
 (unsigned long) [myInt retainCount]);
 [pool drain];
 return 0;
 }

Program Output

myInt retain count = 1
after adding to array = 2
after asssignment to myInt2 = 2
myInt after retain = 3
myInt2 after retain = 3
after release = 2
after removal from array = 1

UPDATE

The following code was taken from the Apples memory management document. They have retained a NSNumber object and its never been released, is this OK ?

- (void)setCount:(NSNumber *)newCount {
[newCount retain];
[_count release];
// Make the new assignment.
_count = newCount;
}
  • 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-26T22:12:36+00:00Added an answer on May 26, 2026 at 10:12 pm

    You shouldn’t be worried about the retain count of your objects, especially because the the NSArray or other objects might retain and then only later release things you pass to them. I’d strongly suggest focusing instead on Objective-C and Cocoa’s memory management rules, which ensure that things are cleaned up when needed.

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

Sidebar

Related Questions

I need to know how to exclude files/folders from php generated zip. Here is
I have logged FBA user in Sharepoint 2010 with FBA and need know what
I need to know which gtktreeview was passed to a function from the handler
I want to make a list view of names. But I need know how
I need to know if i can safely use normal string or I have
I need to know that how to read the directory path from a .properties
I need to know what version of reportlab I'm running. The following doesn't seem
I need to know how to extract raw voice data from android real time.
I know the basic principles about memory management (retain count, autorelease pools etc) in
I need know how can I distribute the project. I am receiving the following

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.