Discussion:
Is there any memory leak in the code while being busy?
liubin liu
2010-04-23 08:41:00 UTC
Permalink
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));
}
--
View this message in context: http://old.nabble.com/Is-there-any-memory-leak-in-the-code-while-being-busy--tp28337646p28337646.html
Sent from the SQLite mailing list archive at Nabble.com.
Marcus Grimm
2010-04-23 09:23:57 UTC
Permalink
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 ?
Post by liubin liu
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));
}
liubin liu
2010-04-23 12:16:39 UTC
Permalink
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.
Post by Marcus Grimm
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 ?
Post by liubin liu
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
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.
Simon Slavin
2010-04-23 12:27:29 UTC
Permalink
Post by liubin liu
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.
It is okay for both sqlite3_prepare_v2() and sqlite3_step() to use memory, but all the memory they use should be released by sqlite3_finalize(). So ignore any information you get until you have correctly called the _finalize() function.

Similarly, opening a database may use up memory, but calling _close() should free it all.

Simon.
liubin liu
2010-04-24 02:29:27 UTC
Permalink
the code's test result in my first post is :

pthread2: prepare: 0, p_stmt: 0xb6a00de0, errmsg: not an error
pthread2: step: 5, p_stmt: 0xb6a00de0, errmsg: database is locked
pthread2: finalize: 5, p_stmt: 0xb6a00de0, errmsg: database is locked
pthread2: finalize: 21, p_stmt: 0xb6a00de0, errmsg: database is locked

...
... (same as upper, nine times)
...

pthread2: close: 0, p_stmt: 0xb6a00dd0, errmsg: library routine
called out of sequence

_______________________________________________________
From the result, could I suppose that the sqlite3_finalize()'s return code
is wrong, and the memory pointed by p_stmt isn't freed by
sqlite3_finalize()?
Post by liubin liu
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.
It is okay for both sqlite3_prepare_v2() and sqlite3_step() to use memory,
but all the memory they use should be released by sqlite3_finalize(). So
ignore any information you get until you have correctly called the
_finalize() function.
Similarly, opening a database may use up memory, but calling _close() should free it all.
Simon.
_______________________________________________
sqlite-users mailing list
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--tp28337646p28347933.html
Sent from the SQLite mailing list archive at Nabble.com.
Black, Michael (IS)
2010-04-23 13:32:48 UTC
Permalink
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: sqlite-users-bounces-CzDROfG0BjIdnm+***@public.gmane.org on behalf of liubin liu
Sent: Fri 4/23/2010 7:16 AM
To: sqlite-users-CzDROfG0BjIdnm+***@public.gmane.org
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.
Post by Marcus Grimm
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 ?
Post by liubin liu
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
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.
liubin liu
2010-04-24 02:19:21 UTC
Permalink
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.
Post by Black, Michael (IS)
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
________________________________
Sent: Fri 4/23/2010 7:16 AM
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.
Post by Marcus Grimm
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 ?
Post by liubin liu
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
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
--
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
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
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.
Black, Michael (IS)
2010-04-24 12:25:41 UTC
Permalink
Well your test code has no leak with the free added -- so you need to post better test code to demonstrate your leak.
Otherwise the suspicion would be with YOUR code.

Michael D. Black
Senior Scientist
Northrop Grumman Mission Systems


________________________________

From: sqlite-users-bounces-CzDROfG0BjIdnm+***@public.gmane.org on behalf of liubin liu
Sent: Fri 4/23/2010 9:19 PM
To: sqlite-users-CzDROfG0BjIdnm+***@public.gmane.org
Subject: Re: [sqlite] Is there any memory leak in the code while being busy?




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.
Post by Black, Michael (IS)
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
________________________________
Sent: Fri 4/23/2010 7:16 AM
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.
Post by Marcus Grimm
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 ?
Post by liubin liu
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
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
--
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
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
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.

Loading...