2011-07-05 15 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這是造成你的內存泄漏?你不使用屬性訪問器/增變器來設置你的類屬性,而是使用除了你的方法之外的方法釋放它們,這會導致問題。至少,您違反了基本的內存管理規則,分配eventString,detailsStringdate,initEvent(它是所有者,因爲您沒有使用屬性訪問器/增變器)並在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調用,並將它們添加到dealloc方法,按照標準的內存管理模式。

試試看看是否有幫助。讓我知道我是否可以澄清任何事情。

0

感謝您的解決方案。它效果很好。事實上不是描述懷特洛克萊是泄漏的原因,但我自己的記憶規則實施(是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回溯是240 kb。我爲此附上了一個屏幕截圖。有沒有問題與實際調用initWithContentsOfFile,但與tableView是不是正確解除分配?(我的類是一個tableViewController)。

謝謝, 亞歷克斯。 instruments

+0

從代碼中很難判斷,但看起來基本正確。由於您將'tableView'視爲類屬性,因此請確保您在'dealloc'方法中釋放'tableView'。 如果您需要更深入的幫助,我會建議開始一個新問題。 – Kongress