NSDate hates itself? UIDatePicker Question

Discussion in 'iOS Programming' started by Buschmaster, Sep 10, 2008.

  1. Buschmaster macrumors 65816

    Buschmaster

    Joined:
    Feb 12, 2006
    Location:
    Minnesota
    #1
    So here's my situation.

    I have one UIDatePicker but it sets two different things. These two things are controlled by a Segmented controller. If it's on one side you're setting one date and if it's on the other side you're setting a different date. What I want it to do is save the date when you change the segmented controller. So first I have to save those dates. So when the datepicker is changed here is what is called:
    Code:
    -(void) pickerChangedDate:(id)sender {
    	if([segmentedControl selectedSegmentIndex] == 0) {
    		NSDate *aDate = [[NSDate alloc] init];
    		aDate = [datePicker date];
    		dateOne = aDate;
    		[aDate release];
    	} else {
    		NSDate *aDate = [[NSDate alloc] init];
    		aDate = [datePicker date];
    		dateTwo = aDate;
    		[aDate release];
    	}			   
    }
    So what I'm doing here is seeing which side needs to be set. When that side is determined I go ahead and try and save it as dateOne or dateTwo which are NSDate properties. This caused no errors but this did. This is what's called when the segmented controller changes:

    Code:
    -(void) segmentedChanged:(id)sender {
    	if([segmentedControl selectedSegmentIndex] == 0) {
    		[datePicker setDate:dateOne animated:YES];
    	} else {
    		[datePicker setDate:dateTwo animated:YES];
    	}	
    }
    Any ideas as to why this would cause a crash?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You code makes no sense.

    I have annotated it with comments:

    Code:
    -(void) pickerChangedDate:(id)sender {
    	if([segmentedControl selectedSegmentIndex] == 0) {
    		NSDate *aDate = [[NSDate alloc] init];
                    // This is an error: why are you creating an object (alloc/init pair)
                    // when you then set the variable to another date in the next
                    // line.  This will leak memory.
    		aDate = [datePicker date];
    		dateOne = aDate;
                    // Where to you retain aDate?
                    // You've date dateOne to point at it, but as it's autoreleased it
                    // will get dealloced real soon.  If you use dateOne after that
                    // you will crash.
                    // Also if dateOne is a property you are doing it all wrong: you
                    // need to either use the date syntax or the setter directly
                    // otherwise any existing object pointed to by dateOne will not
                    // get released and you will leak more memory
    		[aDate release];
                    // Why are you releasing this date?
                    // The date from datePicker will be autoreleased.  You should not release it.
    	} else {
    		NSDate *aDate = [[NSDate alloc] init];
    		aDate = [datePicker date];
    		dateTwo = aDate;
    		[aDate release];
    	}			   
    }
    
    Corrected code (assumes dateOne and dateTwo are properties with synthesized accessor/setter methods)
    Code:
    -(void) pickerChangedDate:(id)sender {
    	if([segmentedControl selectedSegmentIndex] == 0) {
    		self.dateOne = [datePicker date];
    	} else {
    		self.dateTwo = [datePicker date];
    	}			   
    }
    
    Please read up on Cocoa memory management. It's clear you don't follow what you are required to do when you take ownership of an object.
     
  3. Buschmaster thread starter macrumors 65816

    Buschmaster

    Joined:
    Feb 12, 2006
    Location:
    Minnesota
    #3
    Thanks for the help. I could've sworn I had tried that earlier and it was a no-go. That's why I was trying to create another object and set it as that.

    Would the use of self.dateOne rather than just dateOne have made a big difference?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Code:
    Would the use of self.dateOne rather than just dateOne have made a big difference?
    Of course. That's why you need to read the memory management documentation and the properties section in the ObjC-2.0 docs.

    You shouldn't cross-post your questions to more than one list. That is rude.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    Yes, this calls the synthesized setter that will retain the object and release any existing object value for this property.
     

Share This Page