I have recently inherited a large project using an sqlite3 database and currently I am muscling through a significant amount of memory leaks scattered throughout. A couple of the leaks have me confused and demoralised after not being able to solve them. This loop within a method leaks an awful amount of bytes but appears to be so simplistic I simply don’t know how to change it to prevent the leak.
...
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
Almost all the leaks within this datasource class come from this loop.
I’m hoping this is something trivial that my inexperience has overlooked.
Thanks for taking the time to look at my question.
There’s no dealloc in your GraphData class.
(I assume you’re not using ARC because you have
releasein your first code snippet. I really recommend converting to ARC – this kind of leak vanishes.)