2011-07-05 2 views
2

enter image description here 악기를 사용하여 메모리 누수와 관련하여 이상한 문제가 있음을 발견했습니다. 내 앱에는 앱을 통한 이벤트 및 서버와의 전체 통신 (요청 - 응답)을 기록하는 로그 메커니즘이 있습니다. 작성된 각 이벤트 오브젝트에는 시간 소인이 있습니다. 다음과 같이 타임 스탬프는 얻을 수있다 :[NSDate descriptionWithLocale : [NSLocale systemLocale]] 누출?

[NSDate descriptionWithLocale:[NSLocale systemLocale]] 

악기를 사용하여, 나는 그 descriptionWithLocale 원인 누수를 보았다.

-(id)initEvent:(NSString*)eventStr details:(NSString*)detailsStr{ 
    eventString = [[NSMutableString alloc] initWithString:eventStr]; 
    detailsString = [[NSString alloc] initWithString:detailsStr]; 
    date =[[NSMutableString alloc] init]; 
    NSDate* currentDate = [NSDate date];  
    NSString *dateDescStr = [currentDate descriptionWithLocale:[NSLocale systemLocale]]; 
     [date appendString:dateDescStr]; 

    return [super init]; 

감사합니다, 알렉스 : 아래

는 코드입니다.

답변

1

메모리 누수의 원인이되는 descriptionWithLocale이 맞습니까? 속성 접근 자/변경자를 사용하여 클래스 속성을 설정하지 않고 문제를 일으킬 수있는 dealloc 메소드가 아닌 다른 메소드에서이를 해제합니다. 최소한 eventString, detailsStringdateinitEvent (속성 접근 자/변경자를 사용하지 않으므로 소유자)에 할당하고 eventDictionary 메서드에서 해제하여 기본 메모리 관리 규칙을 위반합니다. 소유자가 아닙니다. 나는 다음과 같은 시도하여 시작할 것

(가정의 모든 속성은 retain 속성이) :

-(id)initEvent:(NSString*)eventStr details:(NSString*)detailsStr{ 
    this.eventString = [[NSMutableString alloc] initWithString:eventStr]; 
    this.detailsString = [[NSString alloc] initWithString:detailsStr]; 
    this.date =[[NSMutableString alloc] init]; 
    NSDate* currentDate = [NSDate date];  
    NSString *dateDescStr = [currentDate descriptionWithLocale:[NSLocale systemLocale]]; 
     [date appendString:dateDescStr]; 
    [eventString release]; 
    [detailsString release]; 
    [date release]; 

    return [super init]; 
} 

또한 당신의 방출이 eventDictionary에서 호출을 제거하고 표준 메모리 관리에 따라는 할당 해제의 방법에 추가 무늬.

시도해보고 도움이되는지 확인하십시오. 내가 무엇인가를 명확히 할 수 있는지 알려줘.

0

귀하의 솔루션에 감사드립니다. 그것은 위대한 작품. 실제로 descriptionWithLocale이 누설의 원인 이었지만 내 기억 규칙 구현 (1 년 전 :()이었습니다. 악기가 해당 메서드 호출을 가리키고 있다는 이상한 점 ... .... 지금 내 수업이 어떻게 보이는지 보아주십시오 :

@implementation EventItem 
@synthesize eventString, detailsString, date; 


-(id)initEvent:(NSString*)eventStr details:(NSString*)detailsStr{ 

    if((self = [super init])){ 
     self.eventString = [[NSMutableString alloc] initWithString:eventStr]; 
     self.detailsString = [[NSString alloc] initWithString:detailsStr]; 
     self.date =[[NSMutableString alloc] init]; 
     NSDate* currentDate = [NSDate date];  
     NSString *dateDescStr = [currentDate descriptionWithLocale:[NSLocale systemLocale]]; 
     [date appendString:dateDescStr];  
     [eventString release]; 
     [detailsString release]; 
     [date release]; 
    } 

    return self; 
} 

-(NSMutableDictionary*)eventDictionary{ 
    if(!dictionary) 
     dictionary = [[NSMutableDictionary alloc] init]; 
    else 
     return dictionary; 

    [dictionary setObject:self.date forKey:@"Event"]; 
    [dictionary setObject:self.date forKey:@"Date"]; 
    [dictionary setObject:self.detailsString forKey:@"Details"]; 
    [dictionary setObject:self.eventString forKey:@"EventDescription"]; 

    return [dictionary autorelease]; 
} 

-(void)dealloc{  
    // NSLog(@"dealloc called in EventItem"); 

    [eventString release]; 
    [detailsString release]; 
    [date release]; 

    [super dealloc]; 
} 


@end 
"loadWithContentsOfFile :"

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
    self.tableView.rowHeight = OC_CELL_HEIGHT; 
    self.tableView.backgroundColor = [UIColor clearColor]; 

    UIImage* backgroundImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TableBg" ofType:@"png"]]; 
    UIImageView* background = [[UIImageView alloc] initWithFrame:self.tableView.frame]; 
    background.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    background.image = backgroundImage; 
    [background sizeToFit]; 
    [backgroundImage release]; 
    [self.tableView setBackgroundView:background]; 
    [background release]; 
    [self.tableView setAutoresizesSubviews:YES]; 
    selectedOCData = nil; 
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 
} 

라인 :

이제, 내가 가진 또 다른 문제를 통해로드 일부 이미지에 관련되어

UIImage* backgroundImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TableBg" ofType:@"png"]]; 

누출 98 %와 haviest 역 추적입니다 240kb. 이 도구로 스크린 샷을 첨부했습니다. initWithContentsOfFile의 실제 호출이 아니라 tableView가 올바르게 할당 해제되지 않은 문제가 있습니까? (내 클래스는 tableViewController 임).

감사합니다. 알렉스. instruments

+0

그 코드에서 알기는 어렵지만 기본적으로 정확합니다. 'tableView'를 클래스 프로퍼티로 다루기 때문에'dealloc' 메쏘드에서'tableView'의 할당을 해제해야합니다. 좀 더 자세한 도움이 필요하면 새로운 질문을 시작하는 것이 좋습니다. – Kongress