liubin liu
2010-04-23 08:41:00 UTC
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));
}
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.
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.