2011-10-07 6 views
1

오류 메시지가 표시되지만이를 제거하는 방법을 모르 십니다.xcode, alloc에 ​​대한 오류를 분석하십시오.

-- Method returns an Objective-C object with a +1 retain count (owning reference) 

라인 AAA 및 BBB

+ (CustConf*) initEmptyCustConf { 
    CustConf* theObject = [[CustConf alloc] init]; 
    theObject.ID = 0; 
    theObject.aAA= [[NSString alloc] initWithString:@""]; 
    theObject.Number_Ndx = 0; 
    theObject.bBB = [[NSString alloc] initWithString:@""]; 

    return [theObject autorelease]; 
} 

답변

1

위한

--Object allocated on line 46 is not referenced later in this execution path and has a retain count of +1 (object leaked) 

[[NSString alloc] initWithString:@""];는 불필요하다. 그냥 @""을 사용하십시오.

initEmptyCustConf이로 변경

: 난 당신이 CustConf 클래스에 대한 유지 속성을 정의했다고 가정하고

+ (CustConf *) initEmptyCustConf { 
    CustConf *theObject = [[CustConf alloc] init]; 
    theObject.ID = 0; 
    theObject.aAA = @""; 
    theObject.Number_Ndx = 0; 
    theObject.bBB = @""; 
    return [theObject autorelease]; 
} 
+0

왜 그런가? (미안 미안 기본이 경우) [CustConf alloc] init에서 오는 객체는 자동으로 retaincount를 1로 유지합니까? – Franck

+0

잘 모르겠습니다. CustConf.m 파일의 코드를 표시 할 수 있습니까? 그렇다면 볼 수는 있지만 배부하는 것은 1 유지 카운트를 주어야하며, 자동 리 레이스 (올바른 작업)는 앞으로 언젠가 -1이 될 것입니다. 그래서 모든 것이 정확합니다. 그것은'CustConf' 클래스의'alloc' 또는'init' 메쏘드 안에 있어야합니다. – chown

+0

질문에 대한 theObject.aAA = @ ""대 theObject.aAA = [[NSString 할당] initWithString : @ ""] 값을 설정해야하더라도 할당 할 필요가 없습니까? [[NSString alloc] initWithString : @ "My int value"]; – Franck

1

. theObject는 문자열 aAA와 bBB를 자동으로 유지하고 메서드가 끝나기 전에 코드에서 해제하지 않았기 때문에 메모리가 누수됩니다.

+ (CustConf*) initEmptyCustConf { 
    CustConf* theObject = [[CustConf alloc] init]; 
    theObject.ID = 0; 
    theObject.aAA= [[NSString alloc] initWithString:@""]; //potential leak 
    theObject.Number_Ndx = 0; 
    theObject.bBB = [[NSString alloc] initWithString:@""]; //potential leak 

    return [theObject autorelease]; 
} 

는이 문제를 해결하려면, 당신은 명시 적으로 해제/오토 릴리즈를 통해 theObject.aAA 및 theObject.bBB에 할당 된 문자열을 해제하거나 문자열 상수를 사용해야합니다.

+ (CustConf*) initEmptyCustConf { 

    CustConf* theObject = [[CustConf alloc] init]; 
    theObject.ID = 0; 
    theObject.aAA= @""; 
    theObject.Number_Ndx = 0; 
    theObject.bBB = @""; 

    return [theObject autorelease]; 
} 

또한 당신의 방법은 "초기화"로 시작하는 경우, 유지 개체를 반환하는 사용자 정의, 그래서 마지막에 오토 릴리즈를 제거하거나 방법의 특성을 반영하기 위해 메소드 이름을 변경하십시오.

+0

'init'은 보존 된 객체를 반환하지 않습니다.'alloc','copy'와'new' do. – hypercrypt

+0

실제로 분석기는 이에 대해 불평해야합니다. 그것을 밖으로 시도하십시오. – futureelite7

+0

돌아왔다. 나는 메소드 이름이 좋지 않아서 사용해서는 안되지만 init는 유지 된 객체를 반환하지 않고'self' 또는'nil'의 대용 물인'self'를 반환합니다. 'init'가 보존 된 객체를 반환하면 [[NSObject alloc] init]'은 +2가됩니다 – hypercrypt

관련 문제