2009-06-13 68 views
2

我一直想弄清楚wh。 SQY我的對象分配不斷每次我調用這個函數時分辯起來,儀器報告沒有泄漏,但我按照張貼在這裏的建議得到了很多對象從iphone sqlite3對象分配內存但沒有泄漏

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

& from 

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

未來赫克我試圖解決它:http://www.iphonedevsdk.com/forum/iphone-sdk-development/7092-sqlite3-database-gobbling-up-memory.html

任何幫助表示讚賞一直沒能解決它,我的代碼如下

+(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][email protected]"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]; 
} 

回答

3

看來,那你在每一個循環週期開啓分貝,但收盤只有一次,函數出口之前

所以嘗試改變:

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

 
     sqlite3_close(db); 
    } 

[localPool release]; 
} 

甚至更​​好的變化:

 
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)); 
    }

到:

 

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++) 
    { 
    ... 

是原因你總是打開同一數據庫

+0

同意。在每個循環中打開數據庫是一個非常糟糕的主意。 – 2009-06-16 12:50:07

0

嘗試調用sqlite3_exec有:

pragma cache_size=1 

SQLite的似乎是吞噬內存的緩存。

+0

我同意,在我的應用程序中也看到了這種行爲。 sqlite佔用大量內存來緩存。關閉數據庫並在didReceiveMemoryWarning中再次打開它似乎仍然有效 – lostInTransit 2009-06-13 07:28:02