• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSMutableDictionary not sending release message to contained objects at dealloc time
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableDictionary not sending release message to contained objects at dealloc time


  • Subject: Re: NSMutableDictionary not sending release message to contained objects at dealloc time
  • From: Greg Hurrell <email@hidden>
  • Date: Fri, 11 Apr 2003 20:27:29 +0930

El Friday, 11 Apr, 2003, a las 05:05 Australia/Adelaide, Jonathan E. Jackel escribis:

Just noted weird bug.

I don't think so. You are misunderstanding the scope of your variables, how
autorelease works, or both. Using your second code snippet as an example
(both snippets make the same mistake):


MYObject *obj = [[MYObject alloc] init];
NSMutableDictionary *dict = [NSMutableDictionary
dictionaryWithObjectsAndKeys:obj, @"Key", nil];
NSMutableArray *arr = [[NSMutableArray arrayWithObjects:dict, nil];

NSLog(@"Retain count on obj is %d", [obj retainCount]); // count is 2
(correct)

// remove the dictionary, which should send release to obj as well
[arr removeObjectAtIndex:0];

NSLog(@"Retain count on obj is %d", [obj retainCount]); // count is
still 2 (INCORRECT!)


It's correct, and it's not leaking.

I don't think so!

dict continues to exist throughout the scope of your code. You create it,
add it to an array, then remove it from the array. Its retain count is
incremented, then decremented.

At allocation time, its retain count is 1, even though it is an autoreleased object. It is part of the autoreleased pool, so when the autorelease pool is deallocated, it will send a release message to the dictionary and it will go away (because then its retain count will be zero).

From the Cocoa docs: "You add an object to an autorelease pool so it will receive a release message-and thus might be deallocated-when the pool is destroyed."

None of that affects the objects in dict. Their retain count stays the
same, as you have demonstrated.

It shouldn't. The contents of the dictionary should be sent a release message when the dictionary itself is releaed, to balance the retain message they are sent when added to the dictionary.

Again from the Cocoa docs: "Each corresponding value object receives a retain message to ensure that it won't be deallocated before the dictionary is through with it" and "When an object is removed from an array, it's sent a release message" (note I can't find a quote exactly like this for NSDictionary, only for NSArray, but I believe these two collection classes behave the same way with respect to retain/release of member objects).

This is how I think it works (should work):

- allocate object (retain count 1, regardless of whether autoreleased)
- add object to collection (retain count 2)
- remove object from collection (retain count 1)
- deallocate object when retain count hits 0 again (either via autorelease or explicit release, depending on method with which object was obtained)

The retain count of obj would change only when obj or dict is released,
either explicitly or via autorelease. Neither has happened when your code
reaches the second NSLog statement. Autorelease won't happen until some
time _after_ execution leaves the scope of the code where you declared dict.

As I read the docs, I thought merely removing the object from the dictionary should send it a release message (again, to counteract the retain message sent to it when added).

Cheers
Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: NSMutableDictionary not sending release message to contained objects at dealloc time
      • From: Jonathan Jackel <email@hidden>
    • Re: NSMutableDictionary not sending release message to contained objects at dealloc time
      • From: Greg Titus <email@hidden>
References: 
 >RE: NSMutableDictionary not sending release message to contained objects at dealloc time (From: "Jonathan E. Jackel" <email@hidden>)

  • Prev by Date: Vending Objects in DO
  • Next by Date: Re: Columns with diminishing number of rows?
  • Previous by thread: RE: NSMutableDictionary not sending release message to contained objects at dealloc time
  • Next by thread: Re: NSMutableDictionary not sending release message to contained objects at dealloc time
  • Index(es):
    • Date
    • Thread