iPhone .. NSString正在发布& dealloc - 但我不知道在哪里或为什么

时间:2010-06-21 00:50:33

标签: iphone sdk nsstring release retain

这就是杀了我!

我有一个观点。在.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的所在。

<。>在.m文件中我这样做:

@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陷入困境,直到变量不再需要它...... :(我是否误解了便利方法?

2 个答案:

答案 0 :(得分:6)

你的问题是

 startDate = [thisFormatter stringFromDate:today];

这会为您提供一个自动释放的字符串。你必须保留它。使用

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

或者使用该属性并调用setter:

self.startDate = [thisFormatter stringFromDate:today];

答案 1 :(得分:1)

您需要在viewDidLoad中使用self.startdate设置开始日期 - 这样您将调用访问者并在@property语句中使用retain。如果没有它,你只是直接设置一个值,因为它是一个自动释放的对象,你会在numberOfSectionsInTableView发生之前丢失它。