View Full Version : NSDate hates itself? UIDatePicker Question
Buschmaster
Sep 10, 2008, 10:19 PM
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:
-(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:
-(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
Sep 11, 2008, 04:32 AM
You code makes no sense.
I have annotated it with comments:
-(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)
-(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
Sep 11, 2008, 10:04 AM
You code makes no sense.
I have annotated it with comments:
-(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)
-(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
Sep 11, 2008, 10:12 AM
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
Sep 11, 2008, 10:26 AM
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.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.