Segmentation fault when call PQfinish inside singletone pattern

Viewed 110

I'm create singletone class, and want to add inside PGconn object.

This class, in the past, will provide connections to DB.

// postgresql
#include <libpq-fe.h>
//==============================================================================
class TestST
{
public:
  static TestST& getInstance()
  {
    static TestST test;
    return test;
  }
  bool createConnection()
  {
    mConn.reset( PQsetdbLogin("localhost", "5432", NULL, NULL,
                             "test_db", "user_app",
                             "passwd"),
                 &PQfinish); // [SIGSEGV when delete] passing a reference to the destroy function
    return true;
  }

private:
  TestST()
  {

  }
private:
  std::shared_ptr<PGconn> mConn; // smart pointer to postgres connetion object
};

//==============================================================================
int main(int argc, char** argv)
{
  TestST::getInstance().createConnection(); // create singletone and connection

  return 0;
}
//==============================================================================

When singletone is deleted, function PQfinish calls signal Segmentation fault. If I move the initialization of mConn to the constructor - it's work. If I build and run on windows (MSVC) - it's work.

postgres version 9.6.9; OS - Debian 9; gcc version 6.3.0

0 Answers
Related