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 code....and 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 me....and 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