Is there any memory leak in the normal routine of sqlite3_*()?
        Posted  
        
            by reer
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by reer
        
        
        
        Published on 2010-04-24T08:20:28Z
        Indexed on 
            2010/04/24
            8:23 UTC
        
        
        Read the original article
        Hit count: 243
        
A normal routine of sqlite3_prepare_v2() + sqlite3_step() + sqlite3_finalize() could contain leak.
It sound ridiculous. But the test code seems to say it. Or I used the sqlite3_*() wrongly.
Appreciate for any reply.
__code________________________
include
include // for usleep()
include
int multi_write (int j);
sqlite3 *db = NULL;
int main (void) { int ret = -1;
ret = sqlite3_open("test.db", &db); 
ret = sqlite3_exec(db,"CREATE TABLE data_his (id INTEGER PRIMARY KEY, d1 CHAR(16))", NULL,NULL,NULL); 
usleep (100000); 
int j=0; 
while (1) 
{ 
    multi_write (j++); 
    usleep (2000000); 
    printf ("   ----------- %d\n", j); 
} 
ret = sqlite3_close (db); 
return 0; 
}
int multi_write (int j) { int ret = -1;
char *sql_f = "INSERT OR REPLACE INTO data_his VALUES (%d, %Q)"; 
char *sql = NULL; 
sqlite3_stmt *p_stmt = NULL; 
ret = sqlite3_prepare_v2 (db, "BEGIN TRANSACTION", -1, &p_stmt, NULL); 
ret = sqlite3_step ( p_stmt ); 
ret = sqlite3_finalize ( p_stmt ); 
int i=0; 
for (i=0; i<100; i++) 
{ 
    sql = sqlite3_mprintf ( sql_f, j*100000 + i, "00000000000068FD"); 
    ret = sqlite3_prepare_v2 (db, sql, -1, &p_stmt, NULL ); 
    sqlite3_free ( sql ); 
    //printf ("sqlite3_prepare_v2(): %d, %s\n", ret, sqlite3_errmsg (db)); 
    ret = sqlite3_step ( p_stmt ); 
    //printf ("sqlite3_step():       %d, %s\n", ret, sqlite3_errmsg (db)); 
    ret = sqlite3_finalize ( p_stmt ); 
    //printf ("sqlite3_finalize():   %d, %s\n\n", ret, sqlite3_errmsg (db)); 
} 
ret = sqlite3_prepare_v2 (db, "COMMIT TRANSACTION", -1, &p_stmt, NULL ); 
ret = sqlite3_step ( p_stmt ); 
ret = sqlite3_finalize ( p_stmt ); 
return 0; 
}
__result________________________
And I watch the the process's run by top.
At first, the memory statistics is: PID PPID USER STAT VSZ %MEM %CPU COMMAND 17731 15488 root S 1104 5% 7% ./sqlite3multiwrite
When the printf() in while(1){} of main() prints the 150, the memory statistics is: PID PPID USER STAT VSZ %MEM %CPU COMMAND 17731 15488 root S 1552 5% 7% ./sqlite3multiwrite
It sounds that after 150 for-cycles, the memory used by sqlite3multiwrite increase from 1104KB to 1552KB.
What does it mean? memory leak or other thing?
© Stack Overflow or respective owner