这是杀死我!iPhone .. NSString被释放&dealloc - 但我不知道在哪里或为什么
我有一个看法。在.h文件我这样做:
@interface SearchLogs : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIActionSheetDelegate, UIPickerViewDelegate> {
NSString *startDate;
NSDateFormatter *thisFormatter;
}
@property (nonatomic, retain) NSString *startDate;
@property (nonatomic, retain) NSDateFormatter *thisFormatter;
@end
有在@interface和@properties其他的东西......但是这是我做的startDate
。
我这样做:
@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];
}
这里是我的问题:在numberOfSectionsInTableView
这里我的应用程序崩溃是日志:
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
我的主要问题是为什么?我的代码从未明确发布过startDate
。有没有我正在做的事情导致它不知道它被释放?
TIA
稍向编辑:
我试图取代:
startDate = [thisFormatter stringFromDate:today];
有:
startDate = [[thisFormatter stringFromDate:today] retain];
,它不再崩溃了!我认为NSDateFormatter卡住,直到变量不再需要它... :(我误解便利方法吗?
我(因为你可以看到)终于尝试的(保留)的变量。我没有考虑设置self.VARNAME ..浪费了一小时,试图找出我做错了什么。我需要咖啡因!谢谢! – Jann 2010-06-21 01:07:45