我正在尝试从sqlite中进行一些基本的表检索,并且我一直在寻找一些我无法弄清楚的内存泄漏,特别是因为它只出现在两条等效行中的一条。
我的工作很简单:
创建一个对象(类别)来保存我的sqlite DB行
- 有一个查询sqlite并返回这些objetcs数组的方法
我把所有的代码放在这里(为了简化而将其剥离了一下,但是我的内存泄漏了这个代码)。
category.h:
@interface Category : NSObject {
int catId;
NSString *description;
int order;
NSString *iconName;
}
@property(nonatomic, readwrite, assign) int catId;
@property(nonatomic, retain) NSString *description;
@property(nonatomic, readwrite, assign) int order;
@property(nonatomic, retain) NSString *iconName;
@end
category.m:
#import "Category.h"
@implementation Category
@synthesize catId;
@synthesize description;
@synthesize order;
@synthesize iconName;
@end
检索数组的方法:
-(NSMutableArray *)getAll {
NSString *query = "select id, description, catorder, image from category order by catorder";
sqlite3_stmt *statement;
NSMutableArray *categoryArray = [[NSMutableArray alloc] init];
if (sqlite3_prepare_v2([[Database sharedDatabase] instance], [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int dbId = sqlite3_column_int(statement, 0);
char *dbDescription = (char *)sqlite3_column_text(statement, 1);
int dbOrder = sqlite3_column_int(statement, 2);
char *dbIcon = (char *)sqlite3_column_text(statement, 3);
NSString *desc = [[NSString alloc] initWithUTF8String:dbDescription];
NSString *icon = [[NSString alloc] initWithUTF8String:dbIcon];
Category *category = [[Category alloc] init];
category.catId=dbId;
category.description = desc;
//category.description = @"Test";
category.order = dbOrder;
category.iconName = icon;
//category.icon = @"test.png";
[desc release];
[icon release];
[categoryArray addObject:category];
[category release];
}
sqlite3_finalize(statement);
}
[categoryArray autorelease];
return categoryArray;
}
此行泄露
NSString *icon = [[NSString alloc] initWithUTF8String:dbIcon];
此行不
NSString *desc = [[NSString alloc] initWithUTF8String:dbDescription];
这里有哪些文书显示:
当我向下钻取时:
答案 0 :(得分:2)
您没有dealloc方法。您需要在Category.m文件中包含此内容:
-(void)dealloc
{
[description release];
[iconName release];
[super dealloc];
}
另外,我很确定你不能拥有一个名为“description”的属性。这是一个保留的方法名称。