I created a custom Result set in Resultrow case in sqlite3VdbeExec function as below
custom_pResultSet = (Mem*) malloc(sizeof (Mem) * (custom_numElements)); and I initialized it using sqlite3VdbeMemInit function. for( i = 0 ; i < custom_numElements ; i++ ) { sqlite3VdbeMemInit(&custom_pResultSet[i], p->db, MEM_Null); } I modified the result Custom Result set for select statements and then made pMem to point to it. pMem = p->pResultSet = custom_pResultSet; and then NULL terminated pMem. The program executed fine when executing less number of queries(less than 60) but when large number of queries(more than 60) are executed then I found some memory leak form sqlite3VdbeMemNulTerminate function but the result is found to be correct. ==16096== 160 bytes in 2 blocks are definitely lost in loss record 1 of 1 ==16096== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==16096== by 0x46DACE: sqlite3MemMalloc (sqlite3.c:17131) ==16096== by 0x46E52B: mallocWithAlarm (sqlite3.c:20794) ==16096== by 0x46E5C5: sqlite3Malloc (sqlite3.c:20825) ==16096== by 0x46EE7E: sqlite3DbMallocRaw (sqlite3.c:21191) ==16096== by 0x497721: sqlite3VdbeMemGrow (sqlite3.c:63167) ==16096== by 0x497A50: vdbeMemAddTerminator (sqlite3.c:63278) ==16096== by 0x497ADD: sqlite3VdbeMemNulTerminate (sqlite3.c:63298) ==16096== by 0x4A756D: sqlite3VdbeExec (sqlite3.c:73113) ==16096== by 0x4A00CA: sqlite3Step (sqlite3.c:69462) ==16096== by 0x4A02EC: sqlite3_step (sqlite3.c:69528) The memory leak is increased while increasing the number of rows in the tables. Can anyone kindly explain why there is memory leak when executing large number of queries ??? Or what should be done to correctly create and NULL terminate Custom result set ??