我最近繼承了一個使用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
此數據源類中的幾乎所有泄漏都來自此循環。
我希望這是一些小事,我的經驗不足卻忽略。
感謝您花時間看我的問題。
FYI:'GraphData *項目= [GraphData alloc];'應該是'GraphData * item = [[GraphData alloc] init]'。該代碼如何不會崩潰? – deanWombourne
傑斯,看到我編輯的答案 - @ salo.dm提出了一個非常好的觀點:) – deanWombourne