簡體   English   中英

iPhone .. NSString被釋放和釋放-但是我不知道在哪里或為什么

[英]iPhone .. NSString being released & dealloc — but I cannot tell where or why

這是我的命!

我有一個看法。 在.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一直待到變量不再需要它為止... :(我誤解了便捷方法嗎?

你的問題是

 startDate = [thisFormatter stringFromDate:today];

這為您提供了一個自動發行的字符串。 您必須保留它。 無論使用

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

或使用屬性並調用設置器:

self.startDate = [thisFormatter stringFromDate:today];

您需要在viewDidLoad中使用self.startdate設置開始日期-這樣,您將調用訪問器並在@property語句中使用keep。 否則,您只能直接設置一個值,並且由於它是一個自動釋放的對象,因此在numberOfSectionsInTableView發生之前,您將丟失它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM