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: Jim Correia <email@hidden>
- Date: Sat, 12 Apr 2003 18:41:38 -0400
On Saturday, April 12, 2003, at 06:18 PM, Greg Hurrell wrote:
I cannot believe the number of people who have tried to tell me I
don't understand how autorelease works. These people simply haven't
been reading my code or my comments. Autorelease is one thing that I
actually do understand!
With the way you are sniping at people, it is surprising that anyone
continues to try to help you understand.
The fact that in my code sample I have been using an autoreleased
dictionary and autoreleased array is just a coincidence. I could've
written the same snippet using manually allocate and released items.
The line that was given me trouble is the one where I do a
"removeObjectAtIndex" and the retain count on the removed object did
not go down, which to me seemed both counterintuitive and to run
against what was implied in the Cococ docs.
I think you are still failing to account for who is retaining who at
any point in time.
You are correct, the dictionary is not DEALLOCATED until later (ie.
when it is autoreleased). It should, however, receive a release
message at the time that it is removed from the array, to balance the
retain it received when added to the array.
It did. But if the dictionary isn't going to be dealloced, it has no
reason to release its contents at that point. In fact, to do so would
be wrong.
READ the code, for god's sake. In particular:
I have...
Here is your original code:
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)
the 1st refcount on obj comes from the allocation
the 2nd when it was added to the dictionary
The array now has 2 references - the autoreleased ref from creation and
the array has a reference to it.
// remove the dictionary, which should send release to obj as well
[arr removeObjectAtIndex:0];
The dictionary has been removed from the array, but its reference count
is still 1. It hasn't been deallocted yet.
NSLog(@"Retain count on obj is %d", [obj retainCount]); // count is
still 2 (INCORRECT!)
No - it is correct. The dictionary still has your object in it. It
won't be deallocated until later, at which point your object's
reference count will go back down until one. This is expected.
Conversely, trace the reference count on the objects through this code
snippet.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 0];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]
initWithCapacity: 0];
NSString *string = [NSString stringWithString: @"a string"];
NSLog(@"Strings retain count is %ld, we expect 1", [string
retainCount]);
[dictionary setObject: string forKey: @"myKey"];
NSLog(@"String's retain count is %ld, we expect 2", [string
retainCount]);
[array addObject: dictionary];
// the dictionary is now in the array, and the array is retaining it
// so we release the dictionary
[dictionary release];
dictionary = nil;
// now the only guy holding a reference to the dictionary is the array
// so when we remove the dictionary from the array, it will also
release
// all of its contents
[array removeObjectAtIndex: 0];
NSLog(@"String's retain count is %ld, we expect 1", [string
retainCount]);
[pool release];
return 0;
}
Jim
_______________________________________________
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.