【问题标题】:memory leak in easy sample code简单示例代码中的内存泄漏
【发布时间】:2012-01-21 11:08:36
【问题描述】:

首先,对代码量感到抱歉。 我做错了什么管理内存。我不明白为什么分析器会引发内存泄漏。

@interface obj : NSObject 
{
    NSMutableArray *array;
}
@property (retain, nonatomic) NSMutableArray *array;
@end

@implementation obj
@synthesize array;

- (id)init
{
    self = [super init];
    if (self) 
    {
        // Initialization code here.
        array = [[NSMutableArray alloc] init];
    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
    [array release];
}

@end

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    obj *test = [[obj alloc] init];
    NSNumber *numero = [[NSNumber alloc] initWithFloat:3.4];

    NSLog(@"Número: %g \n", [numero floatValue]);

    [test.array addObject:numero];

    NSLog(@"Numero de elementos: %lu", [test.array count]);

    NSLog(@"Valor: %g", [[test.array objectAtIndex:0] floatValue]);

    NSLog(@"Numero de elementos t2: %lu", [test.array count]);

    numero = [NSNumber numberWithFloat:5.8];

    NSLog(@"Valor t2: %g", [[test.array objectAtIndex:0] floatValue]);
    NSLog(@"Número t2: %g \n", [numero floatValue]);

    [test.array addObject:numero];    

    NSLog(@"Valor: %g", [[test.array objectAtIndex:0] floatValue]);

    [numero release]; **<-- Leak of memory**
    [test release];
    [pool drain];

    return 0;
}

【问题讨论】:

    标签: objective-c memory memory-leaks


    【解决方案1】:

    简单修复,您在重新分配之前忘记释放现有值。

    // You created an instance of NSNumber here
    NSNumber *numero = [[NSNumber alloc] initWithFloat:3.4];
    
    // Then you reassigned it here without releasing it first which caused the leak HERE
    numero = [NSNumber numberWithFloat:5.8];
    
    [numero release]; **<-- Leak of memory**
    

    您可以通过在两个返回自动释放对象的实例中使用 numberWithFloat 来完全解决此问题。

    NSNumber *numero = [NSNumber numberWithFloat:3.4];
    
    numero = [NSNumber numberWithFloat:5.8];
    
    // Now you don't need to release it at all ;)
    //[numero release]; **<-- Leak of memory**
    

    或者您可以通过以下方式修复现有示例:

    NSNumber *numero = [[NSNumber alloc] initWithFloat:3.4];
    
    [numero release];
    
    numero = [NSNumber numberWithFloat:5.8];
    
    // Remove this one since numberWithFloat returns an autoreleased object
    //[numero release]; **<-- Leak of memory**
    

    【讨论】:

    • 感谢您的帮助和很好的解释。我不知道 humberWithFloat 方法是自动释放的。
    【解决方案2】:

    在数组中添加 numero 后,您必须释放 numero,因为 addObject 会在 numero 上调用 Retain。第二个数字 5.8 没问题,因为你不调用 alloc 就可以了。

    【讨论】:

      【解决方案3】:
      1. [super dealloc] 应该始终dealloc 方法中的最后一个调用。
      2. 每当您将变量分配给您拥有所有权的引用时(例如调用 alloc/init 的结果),您必须在重新分配之前释放它。

      NSNumber *numero = [[NSNumber alloc] initWithFloat:3.4];
      ...
      [numero release]; //You must call release before reassigning
      numero = [NSNumber numberWithFloat:5.8];
      ...
      [numero release]; //This is bad because it is now assigned an autoreleased value
      

      现在,对于您的示例,无需分配初始的 numero,只需将其分配为 NSNumber *numero = [NSNumber numberWithFloat:3.4];,就像您执行其余操作一样,然后无需任何调用来释放。

      【讨论】:

        【解决方案4】:

        在您的dealloc 方法中,首先尝试释放array,然后然后调用[super dealloc]。 (通常你应该先释放你的 ivars,然后再调用超类的 dealloc 方法。)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-03
          • 2015-01-24
          • 2019-03-28
          • 1970-01-01
          相关资源
          最近更新 更多