我有一席之地,一开始便宣布“价值”为NSObject,然后在“价值”内实际生成开关说明。 “价值”可以是NSNumber、NSDate、NSData、NSString或Nil。 这是法典:
for (int i = 0; i < self.columnCount; i++) {
NSObject *value;
switch (mysql_fields[i].type) {
case ...
...
case MYSQL_TYPE_LONG:
value = [[NSNumber alloc] initWithInt:atoi(row[i])];
/* [value autorelease]; */ // 2)
break;
case MYSQL_TYPE_DECIMAL:
NSString *decimal = [[NSString alloc] initWithCString:(char *)row[i] encoding:NSUTF8StringEncoding];
value = [[NSDecimalNumber alloc] initWithString:decimal];
/* [value autorelease]; */ // 2)
[decimal release];
break;
case ...
...
} // end of switch
} end of for
Field *field = [[[Field alloc] initWithRecord:record] autorelease];
/* [value autorelease]; */ // 3)
[field setValue:value forKey:@"value"];
/* [value release]; */ // 4)
现在我不知道如何释放“价值”。 这是我所尝试的,相应的X代码4“Analyzer”电文:
- no release -> "potential leak"
- [value autorelease] after alloc/init within each case statement -> "object sent autorelease too many times"
- [value autorelease] directly before the last use -> "object sent autorelease too many times"
- [value release] after the last use -> "Incorrect decrement of the reference count of an object not owned by the caller at this point"