Re: [sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite code?
Hi Pavel, Many thanks for your comments.I will try put together some better code and if I have any issues I will ask again... Thanks again Lynton -Original Message- From: sqlite-users-boun...@sqlite.org [mailto:sqlite-users-boun...@sqlite.org] On Behalf Of Pavel Ivanov Sent: Tuesday, September 06, 2011 5:20 PM To: General Discussion of SQLite Database Subject: Re: [sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite code? > Well, in the first code example, yes you need to free your peekText pointer. > Since you passed SQLITE_STATIC for the destructor parameter to > sqlite3_bind_text, you should free it before you exit the function and > not before. If you use SQLITE_TRANSIENT, you can free it immediately > after the bind call because SQLite will make its own copy. It's a > leak because you allocated memory on the heap (the memory pointed at > by peekText), and never freed the memory (the pointer itself is > destroyed when the stack frame is reclaimed). Don't do that, Lynton, don't free peekText pointer as it's not allocated from heap, it points to a static area. Concerning your questions: your "code contexts" are not enough to understand will there be a leak at the end of application or not. If you stop your program right after those snippets there definitely will be a leak. But what happens after that in the application can free allocated memory. Overall advice is make sure that for each statement you call sqlite3_finalize(), for each connection you call sqlite3_close() and before exiting application you call sqlite3_shutdown() (don't forget to check all result codes and process any errors appropriately). If there is still some leaks reported try to minimize amount of code that reproduces the leak report down to several lines. Post that code here in full along with the leak report and we'll see if we can explain that. Pavel On Tue, Sep 6, 2011 at 11:05 AM, Michael Stephenson wrote: > Well, in the first code example, yes you need to free your peekText pointer. > Since you passed SQLITE_STATIC for the destructor parameter to > sqlite3_bind_text, you should free it before you exit the function and > not before. If you use SQLITE_TRANSIENT, you can free it immediately > after the bind call because SQLite will make its own copy. It's a > leak because you allocated memory on the heap (the memory pointed at > by peekText), and never freed the memory (the pointer itself is > destroyed when the stack frame is reclaimed). > > Just glanced briefly at the second code sample. Where is the "handle" > variable declared? > > > -Original Message- > From: sqlite-users-boun...@sqlite.org > [mailto:sqlite-users-boun...@sqlite.org] On Behalf Of Lynton Grice > Sent: Tuesday, September 06, 2011 1:52 AM > To: General Discussion of SQLite Database > Subject: [sqlite] Totalview Debugger & MemoryScape showing leak in my > SQLite code? > > Hi there, > > I am a huge fan of SQLIte and have recently done some code that I am > debugging with TotalView and MemoryScape (http://www.roguewave.com) - > VERY VERY nice debugger, I have used GDB as well alot but Totalview is > simply awesome > > NOTE: C coding is NOT my day job ;-) > > I am using MemoryScape (I suppose the same as Valgrind) to detect > Memory leaks in my code.when I look at the leak detection I can > see I have some small leaks in my SQLIte codeand would love > someone to tell me how I can fix them? > > For example is moans about the following 2 lines below: > > - idx = sqlite3_bind_parameter_index( stmt, ":tid" ); > - rc = sqlite3_step(stmt); > > Here is more of the code context > > int queue_peekByTID(const char *tid, message *msg){ > char *peekText = "SELECT * FROM queue WHERE tid = :tid;"; > const char *value = NULL; > int idx; > int len; > > sqlite3_prepare_v2(handle,peekText,-1,&stmt,0 ); > > idx = sqlite3_bind_parameter_index( stmt, ":tid" ); > sqlite3_bind_text( stmt, idx, tid, -1, SQLITE_STATIC ); > > rc = sqlite3_step(stmt); > if(rc == SQLITE_ROW){ > > > } > > What is wrong with my code above? Must I FREE the char*? Why would > something say it was a "leak"? > > I am also getting it complaining when I do a "sqlite3_finalize(stmt);" > > I another piece of code I am using SQLite to log certain events for > meand complains about the following 3 lines below: > > - rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | > SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); > - rc = sqlite3_exec(handle,journalMode,0,0,0); > - rc
Re: [sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite code?
> Well, in the first code example, yes you need to free your peekText pointer. > Since you passed SQLITE_STATIC for the destructor parameter to > sqlite3_bind_text, you should free it before you exit the function and not > before. If you use SQLITE_TRANSIENT, you can free it immediately after the > bind call because SQLite will make its own copy. It's a leak because you > allocated memory on the heap (the memory pointed at by peekText), and never > freed the memory (the pointer itself is destroyed when the stack frame is > reclaimed). Don't do that, Lynton, don't free peekText pointer as it's not allocated from heap, it points to a static area. Concerning your questions: your "code contexts" are not enough to understand will there be a leak at the end of application or not. If you stop your program right after those snippets there definitely will be a leak. But what happens after that in the application can free allocated memory. Overall advice is make sure that for each statement you call sqlite3_finalize(), for each connection you call sqlite3_close() and before exiting application you call sqlite3_shutdown() (don't forget to check all result codes and process any errors appropriately). If there is still some leaks reported try to minimize amount of code that reproduces the leak report down to several lines. Post that code here in full along with the leak report and we'll see if we can explain that. Pavel On Tue, Sep 6, 2011 at 11:05 AM, Michael Stephenson wrote: > Well, in the first code example, yes you need to free your peekText pointer. > Since you passed SQLITE_STATIC for the destructor parameter to > sqlite3_bind_text, you should free it before you exit the function and not > before. If you use SQLITE_TRANSIENT, you can free it immediately after the > bind call because SQLite will make its own copy. It's a leak because you > allocated memory on the heap (the memory pointed at by peekText), and never > freed the memory (the pointer itself is destroyed when the stack frame is > reclaimed). > > Just glanced briefly at the second code sample. Where is the "handle" > variable declared? > > > -Original Message- > From: sqlite-users-boun...@sqlite.org > [mailto:sqlite-users-boun...@sqlite.org] On Behalf Of Lynton Grice > Sent: Tuesday, September 06, 2011 1:52 AM > To: General Discussion of SQLite Database > Subject: [sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite > code? > > Hi there, > > I am a huge fan of SQLIte and have recently done some code that I am > debugging with TotalView and MemoryScape (http://www.roguewave.com) - VERY > VERY nice debugger, I have used GDB as well alot but Totalview is simply > awesome > > NOTE: C coding is NOT my day job ;-) > > I am using MemoryScape (I suppose the same as Valgrind) to detect Memory > leaks in my code.when I look at the leak detection I can see I have some > small leaks in my SQLIte codeand would love someone to tell me how I can > fix them? > > For example is moans about the following 2 lines below: > > - idx = sqlite3_bind_parameter_index( stmt, ":tid" ); > - rc = sqlite3_step(stmt); > > Here is more of the code context > > int queue_peekByTID(const char *tid, message *msg){ > char *peekText = "SELECT * FROM queue WHERE tid = :tid;"; > const char *value = NULL; > int idx; > int len; > > sqlite3_prepare_v2(handle,peekText,-1,&stmt,0 ); > > idx = sqlite3_bind_parameter_index( stmt, ":tid" ); > sqlite3_bind_text( stmt, idx, tid, -1, SQLITE_STATIC ); > > rc = sqlite3_step(stmt); > if(rc == SQLITE_ROW){ > > > } > > What is wrong with my code above? Must I FREE the char*? Why would something > say it was a "leak"? > > I am also getting it complaining when I do a "sqlite3_finalize(stmt);" > > I another piece of code I am using SQLite to log certain events for > meand complains about the following 3 lines below: > > - rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | > SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); > - rc = sqlite3_exec(handle,journalMode,0,0,0); > - rc = sqlite3_exec(handle,trigger,0,0,0); > > Here is the code context > > int eventLogOpen(char *eventLogName, unsigned int eventLogRetentionPeriod){ > char *eventLogTable = "CREATE TABLE IF NOT EXISTS [log] ( " > "[idx] INTEGER NOT NULL PRIMARY KEY > AUTOINCREMENT, " > "[timestamp] CHAR(25), " > "[timestamp_secs] CHAR(10), "
Re: [sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite code?
Well, in the first code example, yes you need to free your peekText pointer. Since you passed SQLITE_STATIC for the destructor parameter to sqlite3_bind_text, you should free it before you exit the function and not before. If you use SQLITE_TRANSIENT, you can free it immediately after the bind call because SQLite will make its own copy. It's a leak because you allocated memory on the heap (the memory pointed at by peekText), and never freed the memory (the pointer itself is destroyed when the stack frame is reclaimed). Just glanced briefly at the second code sample. Where is the "handle" variable declared? -Original Message- From: sqlite-users-boun...@sqlite.org [mailto:sqlite-users-boun...@sqlite.org] On Behalf Of Lynton Grice Sent: Tuesday, September 06, 2011 1:52 AM To: General Discussion of SQLite Database Subject: [sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite code? Hi there, I am a huge fan of SQLIte and have recently done some code that I am debugging with TotalView and MemoryScape (http://www.roguewave.com) - VERY VERY nice debugger, I have used GDB as well alot but Totalview is simply awesome NOTE: C coding is NOT my day job ;-) I am using MemoryScape (I suppose the same as Valgrind) to detect Memory leaks in my code.when I look at the leak detection I can see I have some small leaks in my SQLIte codeand would love someone to tell me how I can fix them? For example is moans about the following 2 lines below: - idx = sqlite3_bind_parameter_index( stmt, ":tid" ); - rc = sqlite3_step(stmt); Here is more of the code context int queue_peekByTID(const char *tid, message *msg){ char *peekText = "SELECT * FROM queue WHERE tid = :tid;"; const char *value = NULL; int idx; int len; sqlite3_prepare_v2(handle,peekText,-1,&stmt,0 ); idx = sqlite3_bind_parameter_index( stmt, ":tid" ); sqlite3_bind_text( stmt, idx, tid, -1, SQLITE_STATIC ); rc = sqlite3_step(stmt); if(rc == SQLITE_ROW){ } What is wrong with my code above? Must I FREE the char*? Why would something say it was a "leak"? I am also getting it complaining when I do a "sqlite3_finalize(stmt);" I another piece of code I am using SQLite to log certain events for meand complains about the following 3 lines below: - rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); - rc = sqlite3_exec(handle,journalMode,0,0,0); - rc = sqlite3_exec(handle,trigger,0,0,0); Here is the code context int eventLogOpen(char *eventLogName, unsigned int eventLogRetentionPeriod){ char *eventLogTable = "CREATE TABLE IF NOT EXISTS [log] ( " "[idx] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " "[timestamp] CHAR(25), " "[timestamp_secs] CHAR(10), " "[event_cat] CHAR(10), " "[event_tid] CHAR(50), " "[event_bus_ref] CHAR(50), " "[event_msg] TEXT);"; char trigger[2024]; if(eventLogRetentionPeriod > 0){ sprintf(trigger, "DROP TRIGGER IF EXISTS [log_retention]; " "CREATE TRIGGER [log_retention] " "AFTER INSERT ON log " "FOR EACH ROW BEGIN " "DELETE FROM log " "WHERE timestamp_secs < (strftime('%%s', 'now') - %i); " "END;", eventLogRetentionPeriod); } char *journalMode = "PRAGMA journal_mode=wal;"; int successFlag = ERROR; rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); if (rc == OK){ successFlag = OK; p = sqlite3_malloc(256); sqlite3_busy_handler(handle, &eventLoggerBusyHandler, p); sqlite3_free(p); sqlite3_exec(handle,"PRAGMA default_cache_size = 50;",0,0,0); rc = sqlite3_exec(handle,journalMode,0,0,0); if(rc == OK){ rc = sqlite3_exec(handle,eventLogTable,0,0,0); if(rc == OK){ rc = sqlite3_exec(handle,trigger,0,0,0); } } } return successFlag; } Is there anything I can do to prevent these "leaks"? Maybe I need to clean up using some other SQLite functions I am not aware of etc? Thanks for the help ;-) Lynton ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite code?
Hi there, I am a huge fan of SQLIte and have recently done some code that I am debugging with TotalView and MemoryScape (http://www.roguewave.com) - VERY VERY nice debugger, I have used GDB as well alot but Totalview is simply awesome NOTE: C coding is NOT my day job ;-) I am using MemoryScape (I suppose the same as Valgrind) to detect Memory leaks in my code.when I look at the leak detection I can see I have some small leaks in my SQLIte codeand would love someone to tell me how I can fix them? For example is moans about the following 2 lines below: - idx = sqlite3_bind_parameter_index( stmt, ":tid" ); - rc = sqlite3_step(stmt); Here is more of the code context int queue_peekByTID(const char *tid, message *msg){ char *peekText = "SELECT * FROM queue WHERE tid = :tid;"; const char *value = NULL; int idx; int len; sqlite3_prepare_v2(handle,peekText,-1,&stmt,0 ); idx = sqlite3_bind_parameter_index( stmt, ":tid" ); sqlite3_bind_text( stmt, idx, tid, -1, SQLITE_STATIC ); rc = sqlite3_step(stmt); if(rc == SQLITE_ROW){ } What is wrong with my code above? Must I FREE the char*? Why would something say it was a "leak"? I am also getting it complaining when I do a "sqlite3_finalize(stmt);" I another piece of code I am using SQLite to log certain events for meand complains about the following 3 lines below: - rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); - rc = sqlite3_exec(handle,journalMode,0,0,0); - rc = sqlite3_exec(handle,trigger,0,0,0); Here is the code context int eventLogOpen(char *eventLogName, unsigned int eventLogRetentionPeriod){ char *eventLogTable = "CREATE TABLE IF NOT EXISTS [log] ( " "[idx] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " "[timestamp] CHAR(25), " "[timestamp_secs] CHAR(10), " "[event_cat] CHAR(10), " "[event_tid] CHAR(50), " "[event_bus_ref] CHAR(50), " "[event_msg] TEXT);"; char trigger[2024]; if(eventLogRetentionPeriod > 0){ sprintf(trigger, "DROP TRIGGER IF EXISTS [log_retention]; " "CREATE TRIGGER [log_retention] " "AFTER INSERT ON log " "FOR EACH ROW BEGIN " "DELETE FROM log " "WHERE timestamp_secs < (strftime('%%s', 'now') - %i); " "END;", eventLogRetentionPeriod); } char *journalMode = "PRAGMA journal_mode=wal;"; int successFlag = ERROR; rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); if (rc == OK){ successFlag = OK; p = sqlite3_malloc(256); sqlite3_busy_handler(handle, &eventLoggerBusyHandler, p); sqlite3_free(p); sqlite3_exec(handle,"PRAGMA default_cache_size = 50;",0,0,0); rc = sqlite3_exec(handle,journalMode,0,0,0); if(rc == OK){ rc = sqlite3_exec(handle,eventLogTable,0,0,0); if(rc == OK){ rc = sqlite3_exec(handle,trigger,0,0,0); } } } return successFlag; } Is there anything I can do to prevent these "leaks"? Maybe I need to clean up using some other SQLite functions I am not aware of etc? Thanks for the help ;-) Lynton ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite code?
Hi there, I am a huge fan of SQLIte and have recently done some code that I am debugging with TotalView and MemoryScape (http://www.roguewave.com) - VERY VERY nice debugger, I have used GDB as well alot but Totalview is simply awesome NOTE: C coding is NOT my day job ;-) I am using MemoryScape (I suppose the same as Valgrind) to detect Memory leaks in my code.when I look at the leak detection I can see I have some small leaks in my SQLIte codeand would love someone to tell me how I can fix them? For example is moans about the following 2 lines below: - idx = sqlite3_bind_parameter_index( stmt, ":tid" ); - rc = sqlite3_step(stmt); Here is more of the code context int queue_peekByTID(const char *tid, message *msg){ char *peekText = "SELECT * FROM queue WHERE tid = :tid;"; const char *value = NULL; int idx; int len; sqlite3_prepare_v2(handle,peekText,-1,&stmt,0 ); idx = sqlite3_bind_parameter_index( stmt, ":tid" ); sqlite3_bind_text( stmt, idx, tid, -1, SQLITE_STATIC ); rc = sqlite3_step(stmt); if(rc == SQLITE_ROW){ } What is wrong with my code above? Must I FREE the char*? Why would something say it was a "leak"? I am also getting it complaining when I do a "sqlite3_finalize(stmt);" I another piece of code I am using SQLite to log certain events for meand complains about the following 3 lines below: - rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); - rc = sqlite3_exec(handle,journalMode,0,0,0); - rc = sqlite3_exec(handle,trigger,0,0,0); Here is the code context int eventLogOpen(char *eventLogName, unsigned int eventLogRetentionPeriod){ char *eventLogTable = "CREATE TABLE IF NOT EXISTS [log] ( " "[idx] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " "[timestamp] CHAR(25), " "[timestamp_secs] CHAR(10), " "[event_cat] CHAR(10), " "[event_tid] CHAR(50), " "[event_bus_ref] CHAR(50), " "[event_msg] TEXT);"; char trigger[2024]; if(eventLogRetentionPeriod > 0){ sprintf(trigger, "DROP TRIGGER IF EXISTS [log_retention]; " "CREATE TRIGGER [log_retention] " "AFTER INSERT ON log " "FOR EACH ROW BEGIN " "DELETE FROM log " "WHERE timestamp_secs < (strftime('%%s', 'now') - %i); " "END;", eventLogRetentionPeriod); } char *journalMode = "PRAGMA journal_mode=wal;"; int successFlag = ERROR; rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); if (rc == OK){ successFlag = OK; p = sqlite3_malloc(256); sqlite3_busy_handler(handle, &eventLoggerBusyHandler, p); sqlite3_free(p); sqlite3_exec(handle,"PRAGMA default_cache_size = 50;",0,0,0); rc = sqlite3_exec(handle,journalMode,0,0,0); if(rc == OK){ rc = sqlite3_exec(handle,eventLogTable,0,0,0); if(rc == OK){ rc = sqlite3_exec(handle,trigger,0,0,0); } } } return successFlag; } Is there anything I can do to prevent these "leaks"? Maybe I need to clean up using some other SQLite functions I am not aware of etc? Thanks for the help ;-) Lynton ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] Totalview Debugger & MemoryScape showing leak in my SQLite code?
Hi there, I am a huge fan of SQLIte and have recently done some code that I am debugging with TotalView and MemoryScape (http://www.roguewave.com) - VERY VERY nice debugger, I have used GDB as well alot but Totalview is simply awesome NOTE: C coding is NOT my day job ;-) I am using MemoryScape (I suppose the same as Valgrind) to detect Memory leaks in my code.when I look at the leak detection I can see I have some small leaks in my SQLIte codeand would love someone to tell me how I can fix them? For example is moans about the following 2 lines below: - idx = sqlite3_bind_parameter_index( stmt, ":tid" ); - rc = sqlite3_step(stmt); Here is more of the code context int queue_peekByTID(const char *tid, message *msg){ char *peekText = "SELECT * FROM queue WHERE tid = :tid;"; const char *value = NULL; int idx; int len; sqlite3_prepare_v2(handle,peekText,-1,&stmt,0 ); idx = sqlite3_bind_parameter_index( stmt, ":tid" ); sqlite3_bind_text( stmt, idx, tid, -1, SQLITE_STATIC ); rc = sqlite3_step(stmt); if(rc == SQLITE_ROW){ } What is wrong with my code above? Must I FREE the char*? Why would something say it was a "leak"? I am also getting it complaining when I do a "sqlite3_finalize(stmt);" I another piece of code I am using SQLite to log certain events for meand complains about the following 3 lines below: - rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); - rc = sqlite3_exec(handle,journalMode,0,0,0); - rc = sqlite3_exec(handle,trigger,0,0,0); Here is the code context int eventLogOpen(char *eventLogName, unsigned int eventLogRetentionPeriod){ char *eventLogTable = "CREATE TABLE IF NOT EXISTS [log] ( " "[idx] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " "[timestamp] CHAR(25), " "[timestamp_secs] CHAR(10), " "[event_cat] CHAR(10), " "[event_tid] CHAR(50), " "[event_bus_ref] CHAR(50), " "[event_msg] TEXT);"; char trigger[2024]; if(eventLogRetentionPeriod > 0){ sprintf(trigger, "DROP TRIGGER IF EXISTS [log_retention]; " "CREATE TRIGGER [log_retention] " "AFTER INSERT ON log " "FOR EACH ROW BEGIN " "DELETE FROM log " "WHERE timestamp_secs < (strftime('%%s', 'now') - %i); " "END;", eventLogRetentionPeriod); } char *journalMode = "PRAGMA journal_mode=wal;"; int successFlag = ERROR; rc = sqlite3_open_v2(eventLogName,&handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE, NULL); if (rc == OK){ successFlag = OK; p = sqlite3_malloc(256); sqlite3_busy_handler(handle, &eventLoggerBusyHandler, p); sqlite3_free(p); sqlite3_exec(handle,"PRAGMA default_cache_size = 50;",0,0,0); rc = sqlite3_exec(handle,journalMode,0,0,0); if(rc == OK){ rc = sqlite3_exec(handle,eventLogTable,0,0,0); if(rc == OK){ rc = sqlite3_exec(handle,trigger,0,0,0); } } } return successFlag; } Is there anything I can do to prevent these "leaks"? Maybe I need to clean up using some other SQLite functions I am not aware of etc? Thanks for the help ;-) Lynton ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users