how to avoid (potential) memory leak
I have a for loop where at the beginning an NSObject "value" is declared and then a switch statement within "value" is actually created. "value" then can be an NSNumber, an NSDate, an NSData, an NSString or nil. This is the code:
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)
Now I don't know how to release "value". This is what I tried and the corresponding Xcode 4 "Analyzer" messages:
仅在分配对象时添加autorelease,删除其余部分:
value = [[[NSNumber alloc] initWithInt:atoi(row[i])] autorelease];
// ....
value = [[[NSDecimalNumber alloc] initWithString:decimal] autorelease];
你在for循环中声明了value ,所以你必须在开关后面释放它,但是在for循环中。 
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])];
            break;
        case MYSQL_TYPE_DECIMAL:
            NSString *decimal = [[NSString alloc] initWithCString:(char *)row[i] encoding:NSUTF8StringEncoding];
            value = [[NSDecimalNumber alloc] initWithString:decimal];
            [decimal release];
            break;
        case ...
            ...
    }
    Field *field = [[[Field alloc] initWithRecord:record] autorelease];
    [field setValue:value forKey:@"value"];
    [value release];
}
上一篇: 重新分配全局NSString指针
下一篇: 如何避免(潜在)内存泄漏
