Thanks :)

But in my real code, the sqlite3_free(sql) is in the right place. So I think
that there are other reasons causing the leak.





Black, Michael (IS) wrote:
> 
> You need to sqlite3_free(sql) after you use the sql from your
> sqlite3_mprintf().
>  
>          sql = sqlite3_mprintf (sql_f, i);
>          ret = sqlite3_prepare_v2 (db1, sql, -1, &p_stmt, NULL);
>          sqlite3_free(sql);
> 
>  
> Michael D. Black
> Senior Scientist
> Northrop Grumman Mission Systems
>  
> 
> ________________________________
> 
> From: [email protected] on behalf of liubin liu
> Sent: Fri 4/23/2010 7:16 AM
> To: [email protected]
> Subject: Re: [sqlite] Is there any memory leak in the code while being
> busy?
> 
> 
> 
> 
> sorry for multi-send the message.
> 
> I just test the code again. And sqlite3_finalize() may free the memory.
> I'm
> wrong in the first post.
> 
> 
> But I test the routine of sqlite3_prepare_v2() + sqlite3_step() +
> sqlite3_finalize() in my real code.
> 
> And the test result say when sqlite3_step() is shadowed, the leak is zero.
> When doing the sqlite3_step(), the leak is about 1k byte. And another
> curious phenomenon is that while there are many datas in the data file,
> the
> leak is more bigger than while there are few datas in the data file.
> 
> 
> 
> 
> 
> Marcus Grimm wrote:
>>
>> it is not necessary to send your question multible times... ;)
>>
>> to answer: what makes you think that sqlite3_finalize can't
>> free the prepared statement ?
>>
>> liubin liu wrote:
>>> Is there any memory leak in the code?
>>>
>>> Below is the code. Is there any memory leak in the pthread2?
>>>
>>> While pthread1 is using test.db exclusively, the sqlite3_prepare_v2() of
>>> pthread2 still prepares the p_stmt pointer to a piece of memory malloced
>>> by
>>> sqlite3_preapare_v2(). And then the sqlite3_finalize() can't free the
>>> memory
>>> still because pthread1 is using test.db exclusively. Does it cause a
>>> memory
>>> leak?
>>>
>>>
>>>
>>> ______code__________________________________________________________
>>>
>>> #include <stdio.h>
>>> #include <unistd.h>      // for usleep()
>>> #include <sys/time.h>   // for gettimeofday()
>>> #include <pthread.h>
>>> #include <sqlite3.h>
>>>
>>> void pthread1 (void);
>>> void pthread2 (void);
>>>
>>>
>>> int main (void)
>>> {
>>>     int ret = -1;
>>>
>>>     sqlite3 *g_db = NULL;
>>>     ret = sqlite3_open ("test.db", &g_db);
>>>     ret = sqlite3_exec (g_db, "CREATE TABLE t1 (id INTEGER PRIMARY KEY,
>>> d1
>>> TEXT)", NULL,NULL,NULL);
>>>     ret = sqlite3_close (g_db);
>>>
>>>     usleep (500000);
>>>
>>>
>>>     pthread_t pthr1, pthr2;
>>>     ret = pthread_create (&pthr1, NULL, (void *) pthread1, NULL);
>>>     ret = pthread_create (&pthr2, NULL, (void *) pthread2, NULL);
>>>
>>>
>>>     ret = pthread_join (pthr1, NULL);
>>>     printf ("thread1 end\n");
>>>     ret = pthread_join (pthr2, NULL);
>>>     printf ("thread2 end\n");
>>>
>>>     return 0;
>>> }
>>>
>>>
>>> void pthread1 (void)
>>> {
>>>     int ret = -1;
>>>     sqlite3 *db1 = NULL;
>>>     ret = sqlite3_open ("test.db", &db1);
>>>
>>>     char *sql_f = "INSERT OR REPLACE INTO t1 VALUES (%d,
>>> 'aaabbbcccddd1122')";
>>>     char *sql = NULL;
>>>
>>>     sqlite3_stmt *p_stmt = NULL;
>>>
>>>     struct timeval tv1, tv2;
>>>     ret = gettimeofday (&tv1, NULL);
>>>
>>>     ret = sqlite3_prepare_v2 (db1, "BEGIN TRANSACTION", -1, &p_stmt,
>>> NULL);
>>>     ret = sqlite3_step (p_stmt);
>>>     ret = sqlite3_finalize (p_stmt);
>>>
>>>     // n=1000000, test.db is 25843712 bytes, 25M;
>>>     int i=0, n=1000000;
>>>     for (i=0; i<n; i++)
>>>     {
>>>         sql = sqlite3_mprintf (sql_f, i);
>>>         ret = sqlite3_prepare_v2 (db1, sql, -1, &p_stmt, NULL);
>>>         ret = sqlite3_step (p_stmt);
>>>         ret = sqlite3_finalize (p_stmt);
>>>     }
>>>
>>>     printf ("pthread1:   ret: %d\n", ret);
>>>     ret = sqlite3_prepare_v2 (db1, "COMMIT TRANSACTION", -1, &p_stmt,
>>> NULL);
>>>     ret = sqlite3_step (p_stmt);
>>>     ret = sqlite3_finalize (p_stmt);
>>>
>>>     ret = gettimeofday (&tv2, NULL);
>>>     printf ("time is : %ds\n", (int) (tv2.tv_sec - tv1.tv_sec));
>>>
>>>
>>>     ret = sqlite3_close (db1);
>>> }
>>>
>>>
>>>
>>> void pthread2 (void)
>>> {
>>>     int ret = -1;
>>>
>>>     sqlite3 *db2 = NULL;
>>>     ret = sqlite3_open ("test.db", &db2);
>>>
>>>     usleep (2000000);
>>>
>>>
>>>     sqlite3_stmt *p_stmt = NULL;
>>>     int i=0, n=10;
>>>     for (i=0; i<n; i++)
>>>     {
>>>         char *sql_f = "INSERT OR REPLACE INTO t1 VALUES (%d,
>>> 'bbbbbbbbbbbb1122')";
>>>         char *sql = NULL;
>>>
>>>         sql = sqlite3_mprintf (sql_f, i);
>>>         ret = sqlite3_prepare_v2 (db2, sql, -1, &p_stmt, NULL);
>>>         printf ("pthread2:   prepare:  %d,   p_stmt: %p, errmsg: %s\n",
>>> ret,
>>> p_stmt, sqlite3_errmsg(db2));
>>>         ret = sqlite3_step (p_stmt);
>>>         printf ("pthread2:   step:     %d,   p_stmt: %p, errmsg: %s\n",
>>> ret,
>>> p_stmt, sqlite3_errmsg(db2));
>>>         ret = sqlite3_finalize (p_stmt);
>>>         printf ("pthread2:   finalize: %d,   p_stmt: %p, errmsg: %s\n",
>>> ret,
>>> p_stmt, sqlite3_errmsg(db2));
>>>         printf ("\n");
>>>
>>>         usleep (300000);
>>>     }
>>>
>>>     ret = sqlite3_close (db2);
>>>     printf ("pthread2:   close: %d,      p_stmt: %p, errmsg: %s\n", ret,
>>> p_stmt, sqlite3_errmsg(db2));
>>> }
>> _______________________________________________
>> sqlite-users mailing list
>> [email protected]
>> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>>
>>
> 
> --
> View this message in context:
> http://old.nabble.com/Is-there-any-memory-leak-in-the-code-while-being-busy--tp28337646p28340567.html
> Sent from the SQLite mailing list archive at Nabble.com.
> 
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 
> 
> 
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Is-there-any-memory-leak-in-the-code-while-being-busy--tp28337646p28347901.html
Sent from the SQLite mailing list archive at Nabble.com.

_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to