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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:44:55+00:00 2026-05-21T06:44:55+00:00

I’m new to Objective-C. This is my first post here. I created a singleton

  • 0

I’m new to Objective-C. This is my first post here. I created a singleton for the purpose of managing my applications interface to a database. To start out simple, I have used an NSMutableArray as an ivar. As you will see in the code below and the log output, the retain count is 0 before it is assigned to an NSMutableArray object, and then the retain count is 2 after the assignment.

I am not clear why this happens. Is it because the [NSMutableArray arrayWithObject:(id)] creates an object with a retain count of one and then the assignment self.dataList increments the retain count? Is it safe to call release once? That doesn’t seem like the right thing to do.

Here is the source

#import <Foundation/Foundation.h>


@interface DataInterfaceObject : NSObject {
    NSMutableArray *dataList;

}

@property (nonatomic, retain) NSMutableArray *dataList;

+ (id) sharedAlloc;

@end

…

#import "DataInterface.h"

static DataInterfaceObject *sharedDataInterfaceObject = nil;

@implementation DataInterfaceObject

@synthesize dataList;

+ (id) sharedAlloc {    
    @synchronized(self) {
        if (sharedDataInterfaceObject == nil) 
            sharedDataInterfaceObject = [super alloc];
    }
    return sharedDataInterfaceObject;
}

+ (id) alloc {
    return [[self sharedAlloc] init];
}

- (id)init 
{
    @synchronized(self) {
        NSLog(@"In DataInterface init 1, RetainCount for dataList is %d", [self.dataList retainCount]);
        if (dataList == nil) {
            self = [super init];
            if (self) {
                //Instantiate list
                NSLog(@"In DataInterface init 2, RetainCount for dataList is %d", [self.dataList retainCount]);
                self.dataList = [NSMutableArray arrayWithObjects:@"Dog", @"Cat", @"Homer", @"Vanessa", @"Tour Eiffel", @"Ball", @"Lettuce", nil];
                NSLog(@"In DataInterface init 3, RetainCount for dataList is %d", [self.dataList retainCount]);
            }
        }
    }
    return self;
}

- (void)dealloc 
{
    [dataList release];
    [super dealloc];
}

@end

The log shows the following:

2011-04-06 21:18:26.931 jobs[11672:207] initislized
2011-04-06 21:18:26.933 jobs[11672:207] In DataInterface init 1, RetainCount for dataList is 0
2011-04-06 21:18:26.934 jobs[11672:207] In DataInterface init 2, RetainCount for dataList is 0
2011-04-06 21:18:26.934 jobs[11672:207] In DataInterface init 3, RetainCount for dataList is 2
  • 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-21T06:44:56+00:00Added an answer on May 21, 2026 at 6:44 am

    The code:

    [NSMutableArray arrayWithObjects:@"Dog", @"Cat", @"Homer", @"Vanessa",
        @"Tour Eiffel", @"Ball", @"Lettuce", nil];
    

    Creates an autoreleased variable with a retain count of 1 that will be released sometime in the future. Your property also calls retain so the object will have a retain count of 2 when the print statement is called and then will soon be reduced to 1.

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

Sidebar

Related Questions

No related questions found

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.