如何清除sqlite用于iPhone开发的内存?

时间:2013-12-15 07:12:40

标签: iphone objective-c memory-leaks sqlite

我正在将sqlite用于iPhone应用程序但是我注意到无论何时从数据库中检索信息,所使用的内存都会增加,但永远不会再回落。当我检索数千条记录并导致应用程序在使用几分钟后崩溃时,这就成了一个大问题。任何想法如何解决这个问题。

以下是使用sqlite检索信息的一个函数的示例,无论何时调用此函数,内存都会上升大约200kb并且永远不会回落。

// get path to database
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPathString = [documentsDir stringByAppendingPathComponent:@"puzzled.db"];

// init path, database and statement
const char *dbPath = [dbPathString UTF8String];
sqlite3 *personDB = NULL;
sqlite3_stmt *theStatement = NULL;

// array to hold results
NSMutableArray *someArray = [[NSMutableArray alloc] init];

if(sqlite3_open(dbPath, &personDB) == SQLITE_OK){

    // the sattement as char
    const char *select_stmt = [[NSString stringWithFormat:@"SELECT * FROM attempt"] UTF8String];

    // prepare the statement
    if(sqlite3_prepare_v2(personDB, select_stmt, -1, &theStatement, NULL) == SQLITE_OK){

        // for each row in statement
        while(sqlite3_step(theStatement) == SQLITE_ROW){

            int c0 = sqlite3_column_int(theStatement, 0);
            int c1 = sqlite3_column_int(theStatement, 1);
            int c2 = sqlite3_column_int(theStatement, 2);
            int c3 = sqlite3_column_int(theStatement, 3);
            int c4 = sqlite3_column_int(theStatement, 4);
            int c5 = sqlite3_column_int(theStatement, 5);
            int c6 = sqlite3_column_int(theStatement, 6);
            int c7 = sqlite3_column_int(theStatement, 7);
            int c8 = sqlite3_column_int(theStatement, 8);
            int c9 = sqlite3_column_int(theStatement, 9);
            const char *c10 = (const char *)sqlite3_column_text(theStatement, 10);
            const char *c11 = (const char *)sqlite3_column_text(theStatement, 11);
            const char *c12 = (const char *)sqlite3_column_text(theStatement, 12);
            const char *c13 = (const char *)sqlite3_column_text(theStatement, 13);
            const char *c14 = (const char *)sqlite3_column_text(theStatement, 14);

            NSMutableDictionary *rowDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                                  [[NSNumber alloc] initWithInt:c0], @"attempt_id",
                                                  [[NSNumber alloc] initWithInt:c1], @"attempt_puzzle",
                                                  [[NSNumber alloc] initWithInt:c2], @"attempt_rows",
                                                  [[NSNumber alloc] initWithInt:c3], @"attempt_columns",
                                                  [[NSNumber alloc] initWithInt:c4], @"attempt_width",
                                                  [[NSNumber alloc] initWithInt:c5], @"attempt_height",
                                                  [[NSNumber alloc] initWithInt:c6], @"attempt_time_taken",
                                                  [[NSNumber alloc] initWithInt:c7], @"attempt_finished",
                                                  [[NSNumber alloc] initWithInt:c8], @"attempt_rotate",
                                                  [[NSNumber alloc] initWithInt:c9], @"attempt_overlay",
                                                  [[NSString alloc] initWithUTF8String:c10], @"attempt_time_started",
                                                  [[NSString alloc] initWithUTF8String:c11], @"attempt_time_ended",
                                                  [[NSString alloc] initWithUTF8String:c12], @"attempt_style",
                                                  [[NSString alloc] initWithUTF8String:c13], @"attempt_style_path",
                                                  [[NSString alloc] initWithUTF8String:c14], @"attempt_shot_filepath",nil];
            [someArray addObject:rowDictionary];
        }
    }

    select_stmt = nil;

    // finalizes
    sqlite3_reset(theStatement);
    sqlite3_finalize(theStatement);

    // close the database
    sqlite3_close(personDB);
}

dbPath = nil;
personDB = NULL;
theStatement = NULL;

// if this is returned, no memory problems exist
//return [[NSMutableArray alloc] init];

return someArray;

0 个答案:

没有答案