RE: NSMutableDictionary not sending release message to contained objects at dealloc time
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: "Jonathan E. Jackel" <email@hidden>
- Date: Thu, 10 Apr 2003 15:35:15 -0400
>
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.
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.
None of that affects the objects in dict. Their retain count stays the
same, as you have demonstrated.
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.
If you want to test the retain counts, create dict using alloc/init and
explicitly release it immediately before your second NSLog statement.
Jonathan
_______________________________________________
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.