Question

i've been trying to figure out wh. sqy my object allocation keeps rigth up every time i call this function, Instruments reports no leaks but I get a heck of a lot of object coming from

sqlite3_exec --> sqlite3Prepare --> sqlite3Parser --> yy_reduce --> malloc  & also a whole bunch from 

& from

sqlite3Step --> sqlite3VdbeExec --> sqlite3BtreeInsert --> malloc

I tried solving it by following the suggestions posted here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/7092-sqlite3-database-gobbling-up-memory.html but haven't been able to fix it

ANY HELP is appreciated, my code is below

+(void)getDesignationsInLibrary:(NSString *)library
{
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];    
NSString *dbName = @"s8.sqlite";



NSArray *documentPaths = \
NSSearchPathForDirectoriesInDomains \
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = \
[documentPaths objectAtIndex:0];
NSString *databasePath = \
[documentsDir stringByAppendingPathComponent:dbName];

[[DT sharedDT].designationsInLibrary removeAllObjects];

NSString *sqlString;

for(int i=0;i<[[DT sharedDT].typesInLibrary count];i++)
{   

 if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
 {
     if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));
     }
     NSMutableString *lib=[NSMutableString stringWithString:library];

     [lib appendString:@"-"];
     [lib appendString:[[DT sharedDT].typesInLibrary objectAtIndex:i]];

     if([DT sharedDT].sortedBy==@"AISC Default")
     {
         sqlString = [NSString stringWithFormat:@"select DESIGNATION from \"%@\";",lib];
     }
     else
     {
         sqlString = [NSString stringWithFormat:@"select DESIGNATION from \"%@\" order by cast(%@ as numeric) %@;",lib, [DT sharedDT].sortedBy, [DT sharedDT].sortAscDesc]; 
     }

     const char *sql = [sqlString cStringUsingEncoding:NSASCIIStringEncoding];
     sqlite3_stmt *selectstmt;

     if(sqlite3_prepare_v2(db,sql,-1,&selectstmt, NULL)==SQLITE_OK)
     {
            while(sqlite3_step(selectstmt)==SQLITE_ROW)
            {
                [[DT sharedDT].designationsInLibrary addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)]];
            }
            sqlite3_finalize(selectstmt);
         selectstmt=nil;
     }
 }

}   
sqlite3_close(db);
[localPool release];
}
Was it helpful?

Solution

It seems, that you're opening db on every loop cycle, but close only once, before function exit

So try to change:

    }   
sqlite3_close(db);
[localPool release];
}

to

 
       sqlite3_close(db);
   }   

[localPool release];
}

Or even better change:

for(int i=0;i [[DT sharedDT].typesInLibrary count];i++)
{   

 if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
 {
     if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));
     }

to:

if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
{
    if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));     
    }

    for(int i=0;i [[DT sharedDT].typesInLibrary count];i++)
    {   
    ...

because you're always open the same database

OTHER TIPS

Try invoking sqlite3_exec with:

pragma cache_size=1 

Sqlite seems to gobble up memory for caching.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top