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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:24:58+00:00 2026-06-06T06:24:58+00:00

I have a question. I first created an object which extends NSObject, I provided

  • 0

I have a question.

I first created an object which extends NSObject, I provided overrides for the description and dealloc methods. Here’s my Employee.m file:

@implementation Employee
.....

-(NSString *)description
{
    return [NSString stringWithFormat:@"Employ ID: %d has $%d value of assets", [self     employeeID], [self valueOfAssets]];
}

-(void)dealloc
{   
    NSLog(@"deallocating.. %@", self);
    [super dealloc];
}

In my main.m, I first created an NSMutableArray to hold a list of Employee objects:

NSMutableArray *employees = [[NSMutableArray alloc] init];

for (int i =0; i< 10; i++)
{
    // Create an instance of Employee
    Employee *person = [[Employee alloc] init];

    // Give the instance varaible interesting values
    [person setEmployeeID:i];
    [employees addObject: person];
}

and at the end I set employees to nil

employees = nil;

I expected the dealloc method of each Employee object to be called and I would see some logs like:

deallocating.. Employ ID 0 has value.....
deallocating.. Employ ID 2 has value.....
....

However, I didn’t see any logs and if I set a breakpoint on the dealloc method, the breakpoint is never hit.

Any thoughts?

  • 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-06T06:24:59+00:00Added an answer on June 6, 2026 at 6:24 am

    A couple of observations:

    1. person = nil does not release an object in non-ARC code. It will in ARC code (at least if it’s strong).

    2. In ARC, local objects will be released for you automatically when they fall out of scope. In non-ARC, objects falling out of scope will not be released for you (and if you don’t have other references to those objects elsewhere, you’ll end up with a leak).

    3. Adding an item to a mutable array will increase the retain count of the item, so even if you include a release in your non-ARC code, the object won’t be released until the retain count drops to zero (accomplished by not only releasing the person objects after you add them to the array, but also removing them from the array.

    Thus, given that this is non-ARC code, it could be something like:

    - (void)testInNonArcCode
    {
        NSMutableArray *employees = [[NSMutableArray alloc] init]; // employees retain count = +1
    
        for (int i =0; i< 10; i++)
        {
            //create an instance of Employee
            Employee *person = [[Employee alloc] init]; // person retain count = +1
    
            //Give the instance varaible interesting values
            [person setEmployeeID:i];
            [employees addObject: person];  // person retain count = +2
            [person release];  // person retain count = +1 (YOU REALLY WANT TO DO THIS OR ELSE OR NON-ARC PROGRAM WILL LEAK)
            // person = nil;   // this does nothing, except clears the local var that's limited to the for loop scope ... it does nothing to reduce the retain count or improve memory management in non-ARC code, thus I have commented it out
        }
    
        // do whatever you want
    
        [employees removeAllObjects]; // this will remove all of the person objects and they will have their respective retain counts reduced to 0, and therefore the Employee objects will be released
    
        [employees release]; // employees array's own retain count reduced to zero (and will now be dealloced, itself)
    }
    

    In ARC code:

    - (void)testInArcCode
    {
        NSMutableArray *employees = [[NSMutableArray alloc] init]; // employees retain count = +1
    
        for (int i =0; i< 10; i++)
        {
            //create an instance of Employee
            Employee *person = [[Employee alloc] init]; // person retain count = +1
    
            //Give the instance varaible interesting values
            [person setEmployeeID:i];
            [employees addObject: person];  // person retain count = +2
    
            // person = nil;      // this would reduce person retain count to +1 (but unnecessary in ARC because when person falls out of scope, it will have it's retain count automatically reduced)
        }
    
        // do whatever you want
    
        [employees removeAllObjects]; // this will remove all of the person objects and they will have their respective retain counts reduced to 0, and therefore will be released
    
        // [employees release]; // not permitted in ARC
        // employees = nil;     // this would effectively release employees, but again, not needed, because when it falls out of scope, it will be released anyway
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first time posting here, I have a question which I have
This is my first question. I have an app in which i want to
this is my first question.. so, here we go. i have a site, 100%
I have a quick question here. I know that the cakePHP find('first') function returns
Question arising from my first attempt at using an Async object. I have a
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about defining Foreign Key in EF Code First Fluent API.
i have a question about using GalleryView. At first, i set five default images
Hello this is may first question and I have found so far the following
I have a question about Android Market/Google Play license model for paid applications. First

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.