Help understanding why an NSDate is getting deallocated

Discussion in 'iOS Programming' started by John Baughman, May 28, 2010.

  1. John Baughman macrumors member

    Joined:
    Oct 27, 2003
    #1
    I have a method that I am using recursively. I have 2 variables I am using to carry data between the recursive calls. Here is a example of what I am trying to do...

    Code:
    //  TripsViewController.h
    
    
    @interface TripsViewController : UIViewController {  
    	NSString *editType;
    	NSDate *newDate;
    }
    
    
    @property (nonatomic, retain)NSString *editType;
    @property (nonatomic, retain)NSDate *newDate;
    
    - (void)editWith:(NSDate *)pickedDate location:(NSString *)pickedLocation; 
    - (void)datePickerIsDone;
    - (void)locationSearchViewDidDismisswithLocation:(NSString*)location;
    
    @end
    
    
    Code:
    //  TripsViewController.m
    
    @implementation TripsViewController
    @synthesize editType, newDate;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        editType = @"";
        newDate = [[[NSDate alloc] dateWithTimeIntervalSinceReferenceDate:0] retain];
    }
    
    - (void)editWith:(NSDate *)pickedDate location:(NSString *)pickedLocation{
        if([editType isEqualToString:@"Add"]){
            editType = @"Add1";
            [self pickDate];    //pick date method invokes a UIDatePicker with a done button callback to datPickerIsDone
    
        {else if ([editType isEqualToString:@"Add1"]) {
            editType = @"Add2";
            newDate =  [df dateFromString:[df stringFromDate:nsDate]];  //this gives me an NSDate at midnight.  df is set to short date and no time
            NSLog(@"newDate is set properly here: %@", newDate);
            LocationSearchViewController *locationSearchViewController = ...etc.;
            locationSearchViewController.delegate = self;		
    	[self presentModalViewController:locationSearchViewController animated:YES];
    
        }else if ([editType isEqualToString:@"Add2"]) {
            NSLog(@"newDate is dealocated and this line will throw an exception: %@", newDate);
    
    
        }
    }
    
    
    - (void)datePickerIsDone
    {
    	NSDate *datePicked = datePickerView.date;
    	[self closeDatePicker];
    	[self editWith:datePicked location:nil];
    
    
    }
    
    - (void)locationSearchViewDidDismisswithLocation:(NSString*)location {
            [self dismissModalViewControllerAnimated:YES];
    	[self editWith:nil location:location];
    
    }
    
    
    @end
    
    
    Why does the NSString EditType work fine while the NSDate newDate gets deallocated between calls to the recursive method?

    All I need to do is retain newDate after i set it, but I do not understand why is it getting deallocated. I am not touching it anyplace else.

    Thanks for any help.

    John
     
  2. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #2
    1)
    Does
    Code:
    {else
    
    compile? Or was this just a quick trimming of irrelevant code?

    2)
    Code:
        newDate = [[[NSDate alloc] dateWithTimeIntervalSinceReferenceDate:0] retain];
    
    You are synthesizing yet you are allocating the date. Why not go:
    Code:
        self.newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
    
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    It's getting deallocated because you don't own the value stored in newDate. All your uses of newDate refer directly to the synthesized instance variable. They DO NOT use the property setter. To use the property setter, you need to use this notation:
    Code:
    self.newDate = ...;
    
    You are doing exactly the same thing with editType (i.e. assigning a value directly to the ivar, rather than using the property accessor), but because you only assign string literals, the issue of non-ownership is non-fatal. If you changed your code and started using non-literal strings, then the lack of ownership would become a problem.

    It also looks like you mangled your posted code. There's the uncompilable "{else" already noted, but you also never use the pickedDate parameter in editWith:location:, and you have a spurious or specious "nsDate" variable-name in that method.
     
  4. John Baughman thread starter macrumors member

    Joined:
    Oct 27, 2003
    #4
    Ooops, yes a trimming error.

    Ahhh. Just using self, resolved the issue.

    So should I be using self.xxx when referencing all my synthesized variables? In this example should I be using self.editType instead of just editType? Why does the NSString work differently than the NSDate?

    I'm sure this is not the place to ask these questions. Guess I need to go back and reread the memory management stuff.

    Thanks,

    John
     
  5. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #5
    In short, you should use self.yourproperty for that access. Without the self, the compiler is taking your action as direct member variable operations. So any implicit [variable release] is not being done. There may be instances where you want to do that, but it should be rare (I don't want to say never).

    Don't discount the other issues you have on the creation:
    Code:
    [[[NSDate alloc] dateWithTimeIntervalSinceReferenceDate:0] retain];
    
    may be leaking because dateWithTimeIntervalSinceReferenceDate is effectively creating an autoreleased version of the date. Your alloced NSDate never was released. You may also be double retaining the variable if you assign it via the accessor that you declared (retain).
     

Share This Page