2011-03-27 3 views
0

내가 뭘 잘못하고 있는지 파악하기가 힘듭니다. 누군가가 올바른 방향으로 나를 가리킬 수 있기를 바랍니다. 개체 배열이있는 응용 프로그램에서 작업하고 있습니다. 이러한 각 객체는 객체의 배열을 가질 수 있으므로 탐색 목적으로 해당 마스터 객체에 대한 포인터를 갖습니다. 이러한 개체 중 하나의 복사본을 만들려고 할 때 메모리 누수가 발생합니다. iphone copyWithZone memory leak - 도움이 필요합니다.

@interface ListItem : NSObject <NSCopying> { 

    ListItem *MasterItem; 
    NSString *strText; 
    NSMutableArray *listItems; 
    BOOL boolDone; 
    NSDate *itemDate; 

} 

@property (nonatomic, retain) ListItem *MasterItem; 

@property (nonatomic, retain) NSString *strText; 

@property (nonatomic, retain) NSMutableArray *listItems; 

@property (nonatomic, retain) NSDate *itemDate; 

@property BOOL boolDone; 

@end 


@implementation ListItem 
@synthesize strText, listItems, boolDone, MasterItem, itemDate; 

- (id) init 
{ 
    if (self = [super init]) 
    { 
     self.strText = nil; 

     self.listItems = nil; 

     self.itemDate = nil; 

     self.boolDone = FALSE; 

     self.MasterItem = nil; 
    } 
    return self; 

} 


-(id)copyWithZone:(NSZone *)zone 
{ 

    ListItem *another = [[[self class] allocWithZone:zone] init]; 

    another.MasterItem = [MasterItem copyWithZone:zone]; 
    another.listItems = [listItems copyWithZone:zone]; 
    another.strText = [strText copyWithZone:zone]; 
    another.itemDate = [itemDate copyWithZone:zone]; 
    another.boolDone = boolDone; 
    return another; 
} 


-(void) dealloc 
{ 
    if (itemDate != nil) 
     [itemDate release]; 

    if (MasterItem != nil) 
     [MasterItem release]; 

    if (strText != nil) 
     [strText release]; 

    if (listItems != nil) 
     [listItems release]; 

    [super dealloc]; 
} 


@end 

내가이를 호출하고

, 메모리 누수 :

ListItem *itemMasterToSave = [itemMaster copy]; 
[itemMasterToSave release]; 
+0

어떻게 'MasterItem'이 파란색으로 변하는 지 확인해주세요. 그것이 그것이 클래스 이름이라고 생각하기 때문입니다. 규칙을 따르려면'masterItem'이어야합니다. – bbum

답변

2

라인

another.MasterItem = [MasterItem copyWithZone:zone]; 

another.MasterItem = [[MasterItem copyWithZone:zone] autorelease]; 

및 속성 설정의 모든 라인이 될 것이다.

또한 NSDate 속성을 놓치는 것을 잊었습니다.

팁 : 객관적인 c 런타임으로 이미 처리되었으므로 nil 검사를 수행 할 필요가 없습니다.

+0

건배 메이트. 프로모션 코드를 원하십니까? .-) – Tom