[英]iPhone .. NSString being released & dealloc — but I cannot tell where or why
This is KILLING me! 这是我的命!
I have a view. 我有一个看法。 in the .h file i do this:
在.h文件中,我这样做:
@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
. @interface和@properties中还有其他内容...但是那是我做
startDate
。
in the .m file i do this: 在.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];
}
Here is my problem: My app crashes at numberOfSectionsInTableView
这是我的问题:我的应用程序在
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. startDate
从未在我的代码中明确发布。 Is there something I am doing to cause it to be released without knowing it? 我是否正在做一些事情使它不知道被释放?
TIA 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?
我以为NSDateFormatter一直待到变量不再需要它为止... :(我误解了便捷方法吗?
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];
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. 您需要在viewDidLoad中使用self.startdate设置开始日期-这样,您将调用访问器并在@property语句中使用keep。 Without that you are just setting a value directly, and since it is an autoreleased object you are losing it before numberOfSectionsInTableView ever happens.
否则,您只能直接设置一个值,并且由于它是一个自动释放的对象,因此在numberOfSectionsInTableView发生之前,您将丢失它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.