Re: Copying Notification Dictonaries & sortingUsingSelector problems
Re: Copying Notification Dictonaries & sortingUsingSelector problems
- Subject: Re: Copying Notification Dictonaries & sortingUsingSelector problems
- From: Charles Jolley <email@hidden>
- Date: Tue, 25 Jun 2002 13:31:48 -0500
On Tuesday, June 25, 2002, at 12:49 PM, <email@hidden> wrote:
In the init method:
dockMenuItemsExperiments = [[NSMutableDictionary alloc] init];
- (void)updateDockItemsExperiments: (NSNotification *)note{
dockMenuItemsExperiments = [[note object] copy]; // copies the
dictionary
[self updateDockMenu]; // a method to update my dockmenu
}
Is this the correct way, or could I just say:
dockMenuItemsExperiments = [note object]; ? Or doesn't it matter as
long as I dealloc the dockMenuItemsExperiments dictionary when I do not
longer need it?
Well, first off, you have a memory leak here. If you have already set
the value of dockMenuItemsExperiments to the mutable dictionary and then
replace it with your [[note object] copy], the mutable dictionary will
be leaked.
Second, what type is docMenuItemsExperiments? NSMutableDictionary or
NSDictionary? If it is mutable, you should be using -mutableCopy
instead of -copy.
Thirds, I don't know what the object is you are passing, but based on
the code snipped you've passed, it looks like you want
docMenuItemExperiments to hold a copy of the dictionary. If you kept
JUST the object passed and the object is mutable, it could change
underneath you.
Let me suggest the following (assuming docMenuItemsExperiments is
mutable):
- (void)updateDockItemsExperiments: (NSNotification *)note
{
NSMutableDictionary* oldValue = dockMenuItemsExperiments ;
dockMenuItemsExperiments = [[note object] mutableCopy];
[oldValue release] ; // this will clean up any old contents.
[self updateDockMenu]; // a method to update my dockmenu
}
If you know that the old value of docMenuItemsExperiments and new value
of [note object] will always be different, you can dispense with the
oldValue variable and just release the current value before changing to
the new one. Using the approach I've included, however, is a safer if
[note object] and the old value might be the same thing.
-C
_______________________________________________
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.