English 中文(简体)
目标C:在休息室开关——如何避免(潜在)记忆泄露
原标题:Objective C: switch inside loop - how to avoid (potential) memory leak

我有一席之地,一开始便宣布“价值”为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”电文:

  1. no release -> "potential leak"
  2. [value autorelease] after alloc/init within each case statement -> "object sent autorelease too many times"
  3. [value autorelease] directly before the last use -> "object sent autorelease too many times"
  4. [value release] after the last use -> "Incorrect decrement of the reference count of an object not owned by the caller at this point"
问题回答

只有在分配物体时,才会自动释放:

value = [[[NSNumber alloc] initWithInt:atoi(row[i])] autorelease];
// ....
value = [[[NSDecimalNumber alloc] initWithString:decimal] autorelease];

您是在休息室内宣布<代码> 数值,因此,您必须将其“AFTER”改为“INSIDE” 。

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];
}




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签