• 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
NSMutableDictionary not sending release message to contained objects at dealloc time
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSMutableDictionary not sending release message to contained objects at dealloc time


  • Subject: NSMutableDictionary not sending release message to contained objects at dealloc time
  • From: Greg Hurrell <email@hidden>
  • Date: Thu, 10 Apr 2003 23:40:27 +0930

Just noted weird bug.

I have an NSMutableDictionary that gets added to an NSMutableArray.

When I remove the dictionary, it gets a release message, and deallocates itself (good).

But the dictionary doesn't send release messages to all ITS contents, and so they leak (bad)!

It seems selective though. Witness the following sample code:

// put an autoreleased object in the dictionary, then the dictionary in the array
NSString *str = @"An autoreleased string";
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:str, @"Key", nil];
NSMutableArray *arr = [[NSMutableArray arrayWithObjects:dict, nil];

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

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

NSLog(@"Retain count on string is %d", [str retainCount]); // count is 1 (correct)


Now for the same code but instead of an autoreleased object we'll put in something like a non-autoreleased NSObject subclass:

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!)



Can anyone explain this mystery?
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 E. Jackel" <email@hidden>
    • Re: NSMutableDictionary not sending release message to contained objects at dealloc time
      • From: Raphael Sebbe <email@hidden>
  • Prev by Date: Threads, RunLoops and performSelector:
  • Next by Date: Re: NSMutableDictionary not sending release message to contained objects at dealloc time
  • Previous by thread: Re: Threads, RunLoops and performSelector:
  • Next by thread: Re: NSMutableDictionary not sending release message to contained objects at dealloc time
  • Index(es):
    • Date
    • Thread