Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Buschmaster

macrumors 65816
Original poster
Feb 12, 2006
1,306
27
Minnesota
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

Buschmaster

macrumors 65816
Original poster
Feb 12, 2006
1,306
27
Minnesota
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.
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?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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?

Yes, this calls the synthesized setter that will retain the object and release any existing object value for this property.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.