2011-12-15 2 views
1

우선 코드 량은 죄송합니다. 내가 메모리를 잘못 관리하고 있습니다. 분석기가 메모리 누수를 던지는 이유를 이해할 수 없습니다. 당신의 dealloc 방법에서쉬운 샘플 코드에서의 메모리 누수

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

답변

2

쉽게 해결할 수 있습니다. 재 지정하기 전에 기존 값을 놓아 버리는 것을 잊었습니다.

// 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 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** 
+0

감사 :

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** 

또는 당신은에 의해 기존 예를 해결할 수 있습니다. 나는 그 방법이 humberWithFloat가 autoreleased되었다는 것을 몰랐습니다. –

1

, array 먼저 해제 시도하고 다음[super dealloc]를 호출. (보통은 슈퍼 클래스의 dealloc 메서드를 호출하기 전에, 먼저 인스턴스 변수를 해제해야합니다.)

2
  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];로 할당이 필요하지 않습니다.

0

배열에서 numero를 추가 한 후에는 addero가 numero에서 Retain을 호출하기 때문에 numero를 릴리스해야합니다. 두번째 숫자 인 5.8은 alloc을 호출하지 않기 때문에 괜찮습니다.