NSMutableDictionary not sending release message to contained objects at dealloc time
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.