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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:01:47+00:00 2026-05-26T01:01:47+00:00

Here is what I see when I run the following code: // Dealloc on

  • 0

Here is what I see when I run the following code:

// Dealloc on MyClass not called
MyClass* c = [MyClass new];
self.a = [NSMutableArray new];
[self.a addObject:c];
[c release];
[a release];

// Dealloc on MyClass called
MyClass* c2 = [MyClass new];
NSMutableArray* a2 = [NSMutableArray new];
[a2 addObject:c2];
[c2 release];
[a2 release];

// Dealloc on MyClass called
MyClass* c3 = [MyClass new];
a = [NSMutableArray new];
[a addObject:c3];
[c3 release];
[a release];

Why does dealloc not get called in the first example?

EDIT:

I think I was misunderstanding all this. Basically what I need to do is:

NSMutableArray* alocal = [NSMutableArray new]; // alocal has a retain count of 1
self.a = alocal; // a has a retain count of 1
[alocal release]; // alocal has a retain count of 0
[a release]; // a has a retain count of 0

OR

self.a = [[NSMutableArray new] autorelease]; // a has a retain count of 2
[a release]; // a has a retain count of 0

OR

self.a = [NSMutableArray array]; // a has a retain count of 2
[a release]; // a has a retain count of 0

Just as a note, with the autorelease/array options (#2 and #3), a does not get released right away, so if you want more control go with option #1.

Thanks!

  • 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-26T01:01:47+00:00Added an answer on May 26, 2026 at 1:01 am

    Because in the first example, assuming a is a retain/copy property, the array leaks.

    self.a = [NSMutableArray new];
    

    which, by the way, is different from the third example:

    a = [NSMutableArray new];
    

    In the third case, the object is assigned directly to the ivar (no property setter involved).

    Returning to the first example, the method new returns a retained object (+1) and then the property setter retains as well the object (+2). Then, when the property is released (-1), you end up with a +1. This is wrong. You need to balance your retain/releases to end up with a +0.

    A simple solution is to use a convenience constructor:

    self.a = [NSMutableArray array];
    

    In this case, the method array returns an autoreleased object (+0) and the property setter retains the object (+1), which is balanced by the release of the property (-1) and you end up with a +0.


    Also, in the first example, you release the object you set to the property through the ivar:

    [a release];
    

    As a result, the property is now holding a pointer to a potentially deallocated object. Since you relinquished ownership of the object, there is no guarantee that the object will be alive in the future. If the object was in fact deallocated (because it doesn’t have any other owners), your pointer would still point to the memory address in the heap where the object used to live; except that now, that address might contain anything: the old bit pattern of the said object, or an unrelated object, or absolute garbage.

    Nothing good can happen if you send a message to the deallocated object, e.g. it will make your app crash. To avoid this, release the object through the property:

    self.a = nil;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See here: http://code.google.com/p/ie7-js/ Does anyone have any experience or remarks about this javascript? Is
I have the following code (available for a test run here ): <div style=margin:10px>
As you can see here and here I'm not a good friend of asp.net
I have the following code, and it's giving me a parse error when run.
Just run into a tricky NSFetchedResultsController problem. The following code works fine in all
I have run into a rather weird little problem. In the following code I
I have the following code to show a progress ajax icon and then run
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Not sure what is going on. When I perform the following code... it runs

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.