我收到了错误消息,但不知道如何摆脱它。
-- Method returns an Objective-C object with a +1 retain count (owning reference)
和
--Object allocated on line 46 is not referenced later in this execution path and has a retain count of +1 (object leaked)
对于aAA和bBB行
+ ( CustConf* ) initEmptyCustConf {
CustConf* theObject = [[CustConf alloc] init];
theObject.ID = 0;
theObject.aAA= [[NSString alloc] initWithString:@""];
theObject.Number_Ndx = 0;
theObject.bBB = [[NSString alloc] initWithString:@""];
return [theObject autorelease];
}
答案 0 :(得分:1)
[[NSString alloc] initWithString:@""];
是不必要的。只需使用@""
。
将您的initEmptyCustConf
更改为:
+ (CustConf *) initEmptyCustConf {
CustConf *theObject = [[CustConf alloc] init];
theObject.ID = 0;
theObject.aAA = @"";
theObject.Number_Ndx = 0;
theObject.bBB = @"";
return [theObject autorelease];
}
答案 1 :(得分:1)
我假设你已经为CustConf类定义了保留属性。由于对象将自动保留字符串aAA和bBB,并且在方法结束之前尚未在代码中释放它们,因此最终会泄漏内存。
+ ( CustConf* ) initEmptyCustConf {
CustConf* theObject = [[CustConf alloc] init];
theObject.ID = 0;
theObject.aAA= [[NSString alloc] initWithString:@""]; //potential leak
theObject.Number_Ndx = 0;
theObject.bBB = [[NSString alloc] initWithString:@""]; //potential leak
return [theObject autorelease];
}
要解决此问题,您需要通过release / autorelease显式释放分配给theObject.aAA和theObject.bBB的字符串,或者只使用字符串常量。
+ ( CustConf* ) initEmptyCustConf {
CustConf* theObject = [[CustConf alloc] init];
theObject.ID = 0;
theObject.aAA= @"";
theObject.Number_Ndx = 0;
theObject.bBB = @"";
return [theObject autorelease];
}
此外,如果您的方法以“init”开头,则返回保留对象是自定义的,因此请在结尾处删除自动释放,或更改方法名称以反映方法的性质。