Re: [sqlite] Is there any memory leak in the code while being busy?
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-boun...@sqlite.org on behalf of liubin liu Sent: Fri 4/23/2010 9:19 PM To: sqlite-users@sqlite.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. 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: sqlite-users-boun...@sqlite.org on behalf of liubin liu > Sent: Fri 4/23/2010 7:16 AM > To: sqlite-users@sqlite.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. > > > > > > 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 >>> #include // for usleep() >>> #include// for gettimeofday() >>> #include >>> #include >>> >>> 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 (50); >>> >>> >>> 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=100, test.db is 25843712 bytes, 25M; >>>
Re: [sqlite] Is there any memory leak in the code while being busy?
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()? Simon Slavin-3 wrote: > > > On 23 Apr 2010, at 1:16pm, liubin liu wrote: > >> 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 > sqlite-users@sqlite.org > 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. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
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. 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: sqlite-users-boun...@sqlite.org on behalf of liubin liu > Sent: Fri 4/23/2010 7:16 AM > To: sqlite-users@sqlite.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. > > > > > > 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 >>> #include // for usleep() >>> #include// for gettimeofday() >>> #include >>> #include >>> >>> 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 (50); >>> >>> >>> 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=100, test.db is 25843712 bytes, 25M; >>> int i=0, n=100; >>> for (i=0; 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
Re: [sqlite] Is there any memory leak in the code while being busy?
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-boun...@sqlite.org on behalf of liubin liu Sent: Fri 4/23/2010 7:16 AM To: sqlite-users@sqlite.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. 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 >> #include // for usleep() >> #include// for gettimeofday() >> #include >> #include >> >> 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 (50); >> >> >> 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=100, test.db is 25843712 bytes, 25M; >> int i=0, n=100; >> for (i=0; 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 (200); >> >> >> sqlite3_stmt *p_stmt = NULL; >> int i=0, n=10; >> for (i=0; i> { >> char *sql_f = "INSERT OR REPLACE INTO t1
Re: [sqlite] Is there any memory leak in the code while being busy?
On 23 Apr 2010, at 1:16pm, liubin liu wrote: > 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 sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
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 >> #include // for usleep() >> #include// for gettimeofday() >> #include >> #include >> >> 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 (50); >> >> >> 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=100, test.db is 25843712 bytes, 25M; >> int i=0, n=100; >> for (i=0; 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 (200); >> >> >> sqlite3_stmt *p_stmt = NULL; >> int i=0, n=10; >> for (i=0; i> { >> char *sql_f = "INSERT OR REPLACE INTO t1 VALUES (%d, >> '1122')"; >> 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 (30); >> } >> >> ret = sqlite3_close (db2); >> printf ("pthread2: close: %d, p_stmt: %p, errmsg: %s\n", ret, >> p_stmt, sqlite3_errmsg(db2)); >> } > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > 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 sqlite-users@sqlite.
Re: [sqlite] Is there any memory leak in the code ?
On Apr 23, 2010, at 2:34 PM, liubin liu wrote: > > Is there any memory leak in the code? The buffers returned by sqlite3_mprintf(). ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Is there any memory leak in the code while being busy?
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 > #include // for usleep() > #include// for gettimeofday() > #include > #include > > 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 (50); > > > 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=100, test.db is 25843712 bytes, 25M; > int i=0, n=100; > for (i=0; 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 (200); > > > sqlite3_stmt *p_stmt = NULL; > int i=0, n=10; > for (i=0; i { > char *sql_f = "INSERT OR REPLACE INTO t1 VALUES (%d, > '1122')"; > 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 (30); > } > > ret = sqlite3_close (db2); > printf ("pthread2: close: %d, p_stmt: %p, errmsg: %s\n", ret, > p_stmt, sqlite3_errmsg(db2)); > } ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] Is there any memory leak in the code while being busy?
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 #include // for usleep() #include// for gettimeofday() #include #include 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 (50); 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=100, test.db is 25843712 bytes, 25M; int i=0, n=100; for (i=0; ihttp://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. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] Is there any memory leak in the code ?
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 #include // for usleep() #include// for gettimeofday() #include #include 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 (50); 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=100, test.db is 25843712 bytes, 25M; int i=0, n=100; for (i=0; ihttp://old.nabble.com/Is--there--any-memory-leak-in-the-code---tp28337636p28337636.html Sent from the SQLite mailing list archive at Nabble.com. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] Is there any memory leak in the code?
Below is my code. My question is: 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 #include // for usleep() #include// for gettimeofday() #include #include 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 (50); 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=100, test.db is 25843712 bytes, 25M; int i=0, n=100; for (i=0; ihttp://old.nabble.com/Is-there-any-memory-leak-in-the-code--tp28337631p28337631.html Sent from the SQLite mailing list archive at Nabble.com. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users