views:

47

answers:

2

I am checking Day objects in my schedule Dictionary and want to know what to do about the Day pointer when I'm done. Build and Analyze doesn't complain about it, but it just sits there taunting me. I feel like it's just sitting there when the function finishes and maybe even a new one gets created each time through the loop. Also, when releasing day each time through the loop I end up releasing the original object. Any ideas?

- (NSUInteger) showsInTheNext:(NSUInteger)days {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyLLdd"];

    if(days == 0) days = 91;
    NSUInteger shows = 0;

    for (NSUInteger x = 0; x < days; x++)
    {
       Day *day = [self.schedule objectForKey:[dateFormatter stringFromDate:[self.date addTimeInterval:60*60*24*x]]];
       if((day != nil) && ([day.type isEqualToString:@"Show"])) shows++;
       //[day release];
    }

    [dateFormatter release];
    return shows;
}
+1  A: 

The NSDictionary retains the NSObject subclass when it is added to the NSDictionary. When you fetch it back out no additional retain message is sent, so you should discard the pointer without sending a release message.

If the object is removed from the dictionary, the NSDictionary implementation will send a release message to the object being removed.

You can verify this by doing debug-level checks of the retainCount to make sure it doesn't change.

fbrereto
OK, so how do I go about discarding the pointer then? I was thinking that when I make this new pointer, maybe the retain count of the object pointed to gets increased and therefore maybe not released when I eventually remove it from the schedule Dictionary.
rob5408
The pointer you have above is just a pointer - nothing special happens to the object assigned to it when it is assigned. So in this case there is no retain message being sent to the object found out of the dictionary. To discard the pointer you simply do as you are doing in the example you posted: nothing.
fbrereto
woohoo! thanks!
rob5408
+1  A: 

You are simply pulling an instance of Day that already exists from your schedule dictionary. You don't need to (and shouldn't) release it when you are done unless you are removing it from the dictionary. Nowhere in your loop is a new instance of Day being created.

Marc W