【发布时间】:2012-06-16 06:43:14
【问题描述】:
我最近继承了一个使用 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
这个数据源类中的几乎所有泄漏都来自这个循环。
我希望这是我没有经验的微不足道的事情。
感谢您花时间查看我的问题。
【问题讨论】:
-
仅供参考:
GraphData *item = [GraphData alloc];应该是GraphData *item = [[GraphData alloc] init]。该代码如何不崩溃? -
杰斯,看看我编辑的答案 - @salo.dm 提出了一个很好的观点:)
标签: iphone ios memory sqlite memory-leaks