2012-06-13 32 views
0

我最近繼承了一個使用sqlite3數據庫的大型項目,目前我正在通過散佈在整個內存中的大量內存泄漏進行肌肉訓練。一些泄漏使我無法解決它們後感到困惑和沮喪。這個循環內的循環泄漏了大量的字節,但看起來很簡單,我根本不知道如何改變它來防止泄漏。圖數據源中的內存泄漏

... 
    while ((ret=sqlite3_step(selStmt))==SQLITE_ROW) 
    { 

     GraphData *item = [GraphData alloc]; 

     item.key = sqlite3_column_int(selStmt, 0); 
     item.value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selStmt,1)]; 

     [newData addObject:item]; 

     [item release], item = nil; 

    } 
... 

    @interface GraphData : NSObject{ 

    NSInteger key; 
    NSString *value; 
} 

@property (nonatomic, readwrite) NSInteger key; 
@property (nonatomic, retain) NSString *value; 

-(id)initWithPrimaryKey:(NSInteger) xid; 
-(id)initWithName:(NSString *)n key:(NSInteger)i; 

@end 



    #import "GraphData.h" 

@implementation GraphData 

@synthesize key,value; 

-(id)initWithPrimaryKey:(NSInteger) xid{ 

    self.key = xid; 
    self.value = @""; 

    return self; 

} 
-(id)initWithName:(NSString *)n key:(NSInteger)i{ 

    self.key = 0; 
    self.value = n; 

    return self; 

} 

@end 

此數據源類中的幾乎所有泄漏都來自此循環。

我希望這是一些小事,我的經驗不足卻忽略。

感謝您花時間看我的問題。

+0

FYI:'GraphData *項目= [GraphData alloc];'應該是'GraphData * item = [[GraphData alloc] init]'。該代碼如何不會崩潰? – deanWombourne

+0

傑斯,看到我編輯的答案 - @ salo.dm提出了一個非常好的觀點:) – deanWombourne

回答

0

GraphData類中沒有dealloc。

- (void)dealloc { 
    [value release]; 
    [super dealloc]; 
} 

(我假設你沒有使用ARC,因爲你在你的第一個代碼段有release我真的建議轉換爲ARC - 這種泄漏消失。)

+0

正在轉型爲ARC相對直截了當?這個項目運行時間很長,我不是最初的開發者。 – Jace

+0

在遵循這兩條建議之後,泄漏仍然存在,所以我現在將集中精力在其他方面的努力。 – Jace

+0

這可能應該是'[value release];'而不是'value = nil;' –