I have written following code and its working fine, but when I run valgrind to check for leaks then I get a big leak. Don't know what I am doing wrong.
Anyone please help me to understand what I am doing wrong ?
MY SINGLETON CLASS:
Header: SampleClass.h
#ifndef SAMPLECLASS_H
#define SAMPLECLASS_H
struct sqlite3;
class SampleClass
{
private:
static SampleClass *m_pHandle;
sqlite3 *m_pSqlDb;
public:
static SampleClass* getHandle() {
if (!m_pHandle)
{
m_pHandle = new SampleClass();
}
return m_pHandle;
}
static void deleteHandle()
{
delete m_pHandle;
m_pHandle = nullptr;
}
void setDatabase(const sqlite3 *psqldb)
{
if (psqldb)
{
m_pSqlDb = const_cast<sqlite3*>(psqldb);
}
else {
// print error
}
}
private:
SampleClass() : m_pSqlDb(nullptr) {}
~SampleClass() { m_pSqlDb = nullptr; }
SampleClass(const SampleClass&) = delete;
SampleClass& operator=(const SampleClass&) = delete;
SampleClass(SampleClass&&) = delete;
SampleClass& operator=(SampleClass&&) = delete;
};
#endif // SAMPLECLASS_H
Implementaion: SampleClass.cpp
#include "SampleClass.h"
SampleClass* SampleClass::m_pHandle = nullptr;
MY DRIVER PROGRAM: Test.cpp
#include "SampleClass.h"
#include <sqlite3.h>
int main()
{
sqlite3 *mydb;
const int retStatus = sqlite3_open_v2("./CPMDB_M100.db", &mydb, SQLITE_OPEN_READWRITE, nullptr);
SampleClass::getHandle()->setDatabase(mydb);
SampleClass::deleteHandle();
return 0;
}
I am getting leak summary like below in my actual implementation.
> LEAK SUMMARY:
> ==32074== definitely lost: 0 bytes in 0 blocks
> ==32074== indirectly lost: 0 bytes in 0 blocks
> ==32074== possibly lost: 0 bytes in 0 blocks
> ==32074== still reachable: 82,680 bytes in 75 blocks
> ==32074== of which reachable via heuristic:
> ==32074== length64 : 9,968 bytes in 73 blocks
If I remove the deleteHandle() call in main() then all those lost blocks bytes goes under "still reachable" category.
LEAK SUMMARY:
==32095== definitely lost: 872 bytes in 1 blocks
==32095== indirectly lost: 9,016 bytes in 71 blocks
==32095== possibly lost: 0 bytes in 0 blocks
==32095== still reachable: 72,784 bytes in 2 blocks
==32095== of which reachable via heuristic:
==32095== length64 : 80 bytes in 1 blocks
Commands to compile:
g++ -c -std=gnu++11 Test.cpp
g++ -c -std=gnu++11 SampleClass.cpp
g++ -o Test Test.o SampleClass.o -L/usr/include -lsqlite3
Thanks