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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:55:26+00:00 2026-06-13T21:55:26+00:00

Now I have a ClassA : NSObject , and then in the viewcontroller viewDidLoad

  • 0

Now I have a ClassA : NSObject, and then in the viewcontroller viewDidLoad, look at the under code:

- (void)viewDidLoad {
    ClassA *a = [[ClassA alloc] init];
    NSLog(@"a retainCount = %d", [a retainCount]);
    ClassA *b = a;
    NSLog(@"a retainCount = %d b retainCount= %d ", [a retainCount],[b retainCount]);
    [a release];
    NSLog(@"a retainCount = %d b retainCount= %d", [a retainCount],[b retainCount]);
    [super viewDidLoad];
}

The console output looks like:

2012-11-02 14:43:35.437 RetainCountTest[1043:207] a retainCount = 1
2012-11-02 14:43:35.439 RetainCountTest[1043:207] a retainCount = 1 b retainCount= 1  
2012-11-02 14:43:35.439 RetainCountTest[1043:207] a retainCount = 1 b retainCount= 1

I don’t understand that when I call [a release], why the [a retainCount]== 1?

  • 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-13T21:55:27+00:00Added an answer on June 13, 2026 at 9:55 pm

    You should never pay attention to retainCount’s. They are at best confusing, and at worst misleading. One should just make sure that they follow memory management rules of retain/release correctly, and forget about retainCounts.

    From documentation..

    This method is of no value in debugging memory management issues.
    Because any number of framework objects may have retained an object in
    order to hold references to it, while at the same time autorelease
    pools may be holding any number of deferred releases on an object, it
    is very unlikely that you can get useful information from this method.

    EDIT : Suggested Reading

    When to use -retainCount?

    EDIT : After seeing OP’s comment

    From Cocoa Core Memory Management rules

    When you create or copy an object, its retain count is 1. Thereafter
    other objects may express an ownership interest in your object, which
    increments its retain count. The owners of an object may also
    relinquish their ownership interest in it, which decrements the retain
    count. When the retain count becomes zero, the object is deallocated
    (destroyed).

    If one read this, he/she might think, Oh retainCount is godsent, and I can see the complete alloc/retain/release cycle of an object just using an NSLog statement. But it doesn’t actually works that way. You cannot say, you have sole ownership of object you create. That object might be retained by any other framework object. And by releasing you are just relinquishing your ownership. The object will get released only after all other objects remove their reference.

    I don’t know why it remains in public API.

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

Sidebar

Related Questions

Ok, so I'm using Objective-C. Now, say I have: TopClass : NSObject - (int)
I have this simple class TestViewController.h #import <UIKit/UIKit.h> #import ClassB.h @protocol TestViewControllerDelegate <NSObject> -(void)hello;
Say you have class Block Class Block : NSObject Now, you want to create
I have a singleton class with this code: manager.h @interface Manager : NSObject {
I have class Fruit with data and logic. Now I need a lot of
I have class1.m . I declared a method and written in it. Now i
I have a class called Trial which has_many results. Now What I want to
I have a class that creates a file. I am now doing integration tests
Right now I have a function, in a class that is used to listen
I have a class A providing Bitmaps to other classes B, C, etc. Now

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.