2010-06-21 42 views
0

這是殺死我!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

在.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卡住,直到變量不再需要它... :(我誤解便利方法嗎?

回答

6

你的問題是

startDate = [thisFormatter stringFromDate:today]; 

這使你是一個自動釋放的字符串,你必須保留它,可以使用

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

或者使用屬性和調用設置:

self.startDate = [thisFormatter stringFromDate:today]; 
+0

我(因爲你可以看到)終於嘗試的(保留)的變量。我沒有考慮設置self.VARNAME ..浪費了一小時,試圖找出我做錯了什麼。我需要咖啡因!謝謝! – Jann 2010-06-21 01:07:45

1

你需要在viewDidLoad中使用self.startdate設置開始日期 - 這樣你將調用訪問器並使用在@property聲明中保留。如果沒有,你只是直接設置一個值,因爲它是有史以來numberOfSectionsInTableView發生之前,你正在失去它的自動釋放的對象。

相關問題