我目前正在与一些内存泄漏作斗争,并且在解决我剩下的最后一个问题时遇到了一些严重的麻烦。泄漏工具显示了由于各种不同原因而全部来自同一方法的多个泄漏,主要归因于 NSCFString、NSMutableArray 和我创建的一个名为 GraphData 的类。我试图以几种不同的方式修复它但无济于事,因此希望可以对这个问题有所了解,希望这是我忽略的一些简单的事情。
这是一些代码:
// the offending, leaking method
-(NSMutableArray*)fillDataInArray:(NSInteger)keyphrase_id{
NSLog(@"Keyphrase_id:%d", keyphrase_id);
NSDate *startdate = [self getDateForApplicationInstalled];
NSDate *enddate = [NSDate date];
NSString *dateString1=[[NSString alloc] initWithString: [fmt stringFromDate:startdate]];
NSString *dateString2=[[NSString alloc] initWithString: [fmt stringFromDate:enddate]];
NSMutableArray *newDataNew = [[NSMutableArray alloc]init];
self.newData = newDataNew;
[newDataNew release];
selStmt = nil;
if (!selStmt)
{
const char *sql = "select distinct position, key_time from ranking where keyphrase_id = ? and key_time between ? and ? order by key_time";
if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK)
{
selStmt = nil;
}
NSInteger n = keyphrase_id;
sqlite3_bind_int(selStmt, 1, n);
sqlite3_bind_text(selStmt, 2, [dateString1 UTF8String] , -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selStmt, 3, [dateString2 UTF8String] , -1, SQLITE_TRANSIENT);
NSLog(@"SQL query is: [%s]", sql);
}
if (!selStmt)
{
NSAssert1(0, @"Can't build SQL to read keyphrases [%s]", sqlite3_errmsg(database));
}
int ret;
while ((ret=sqlite3_step(selStmt))==SQLITE_ROW)
{
GraphData *item = [[GraphData alloc]init];
item.key = sqlite3_column_int(selStmt, 0);
item.value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selStmt,1)];
[newData addObject:item];
[item release], item = nil;
}
sqlite3_reset(selStmt); // reset (unbind) statement
[dateString2 release];
[dateString1 release];
return newData;
}
//GraphData.h
@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
//GraphData.m
#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;
}
-(void)dealloc{
[value release], value = nil;
[super dealloc];
}
@end
谢谢你看我的帖子!