This is KILLING me!
I have a view. in the .h file i do this:
@interface SearchLogs : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIActionSheetDelegate, UIPickerViewDelegate> {
NSString *startDate;
NSDateFormatter *thisFormatter;
}
@property (nonatomic, retain) NSString *startDate;
@property (nonatomic, retain) NSDateFormatter *thisFormatter;
@end
There are other things in the @interface and @properties ...but that is where i do startDate
.
in the .m file i do this:
@implementation SearchLogs
@synthesize startDate;
@synthesize thisFormatter;
- (void)viewDidLoad {
NSLog(@"viewDidLoad\n");
[super viewDidLoad];
thisFormatter = [[NSDateFormatter alloc] init];
[thisFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *today = [[NSDate alloc] init];
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
startDate = [thisFormatter stringFromDate:today];
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
[today release];
}
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"viewWillAppear\n");
[super viewWillAppear:animated];
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"viewDidAppear\n");
[super viewDidAppear:animated];
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"numberOfSectionsInTableView\n");
// Return the number of sections.
NSLog(@"startDate refcount: '%i'\n",[startDate retainCount]);
return 6;
}
- (void)dealloc {
[startDate release];
[thisFormatter release];
}
Here is my problem: My app crashes at numberOfSectionsInTableView
Here is the log:
2010-06-20 17:35:22.363 cConnect[10529:207] viewDidLoad
2010-06-20 17:35:22.376 cConnect[10529:207] startDate refcount: '0'
2010-06-20 17:35:22.378 cConnect[10529:207] startDate refcount: '1'
2010-06-20 17:35:22.378 cConnect[10529:207] viewWillAppear
2010-06-20 17:35:22.379 cConnect[10529:207] startDate refcount: '1'
2010-06-20 17:35:22.379 cConnect[10529:207] viewDidAppear
2010-06-20 17:35:22.380 cConnect[10529:207] startDate refcount: '1'
2010-06-20 17:35:22.381 cConnect[10529:207] numberOfSectionsInTableView
2010-06-20 17:35:22.381 cConnect[10529:207] *** -[CFString retainCount]: message sent to deallocated instance 0x5da5730
My main question is WHY? startDate
is never explicitly released in my code. Is there something I am doing to cause it to be released without knowing it?
TIA
SLIGHT EDIT:
I tried replacing:
startDate = [thisFormatter stringFromDate:today];
with:
startDate = [[thisFormatter stringFromDate:today] retain];
and it is no longer crashing! I thought NSDateFormatter stuck around until the variable no longer needed it... :( Am I misunderstanding convenience methods?