views:

35

answers:

2

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?

+1  A: 

You need to set start date using self.startdate in viewDidLoad - in this way you will call the accessor and use the retain in your @property statement. Without that you are just setting a value directly, and since it is an autoreleased object you are losing it before numberOfSectionsInTableView ever happens.

h4xxr
+3  A: 

Your problem is

 startDate = [thisFormatter stringFromDate:today];

This gives you an autoreleased string. You must retain it. Either use

startDate = [thisFormatter stringFromDate:today];
[startDate retain];

Or use the property and invoke the setter:

self.startDate = [thisFormatter stringFromDate:today];
Eiko
I (as you can see) finally tried the (retain) on the variable. I did not think about setting self.VARNAME .. wasted an HOUR trying to figure out what I did wrong. I need caffeine! thanks!
Jann