sqlite iOS应用程序内存泄漏

sqlite iOS应用程序内存泄漏,ios,memory,sqlite,nsmutablearray,memory-leaks,Ios,Memory,Sqlite,Nsmutablearray,Memory Leaks,我目前正在努力解决一些内存泄漏问题,并且在解决最后一个内存泄漏问题时遇到了一些严重的问题。leaks工具显示了多个泄漏,这些泄漏都来自同一个方法,原因多种多样,主要归因于NSCFString、NSMutableArray和一个名为GraphData的I类。我试图用几种不同的方法来解决这个问题,但都无济于事,所以希望能对这个问题有所启发,希望这是我忽略的简单问题 下面是一些代码: // the offending, leaking method -(NSMutableArray*)fillData

我目前正在努力解决一些内存泄漏问题,并且在解决最后一个内存泄漏问题时遇到了一些严重的问题。leaks工具显示了多个泄漏,这些泄漏都来自同一个方法,原因多种多样,主要归因于NSCFString、NSMutableArray和一个名为GraphData的I类。我试图用几种不同的方法来解决这个问题,但都无济于事,所以希望能对这个问题有所启发,希望这是我忽略的简单问题

下面是一些代码:

// 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

谢谢你看我的帖子

泄漏工具告诉您泄漏对象的创建位置。由于NSCFString、NSMutableArray和GraphData对象是从该方法中泄漏的,让我们看看这是如何发生的

您只在NSMutableArray中插入GraphData对象(包含字符串对象),它们似乎已被正确释放。因此,要泄漏在该方法中创建的GraphData对象,包含元素的数组必须是泄漏

请检查该方法的调用方。我假设其中一个是保留(而不是释放)方法的返回值

此外,您的初始值设定项必须更改为调用super的init,但这与泄漏无关。例如:

-(id)initWithPrimaryKey:(NSInteger) xid
{
    self = [super init];
    if (self) {
        self.key = xid;
        self.value = @"";
    }
    return self;   
}

方法的调用方似乎保留了方法调用的返回值,所以希望您的答案一针见血!不过,我明天必须检查一下,因为我现在要回家了。谢谢你!