首页 > 解决方案 > 在单音模式内调用 PQfinish 时出现分段错误

问题描述

我正在创建单调类,并想在 PGconn 对象中添加。

过去,此类将提供与 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;
}
//==============================================================================

当单音被删除时,函数 PQfinish 调用信号 Segmentation fault。如果我将 mConn 的初始化移动到构造函数 - 它的工作。如果我在 Windows (MSVC) 上构建和运行 - 它可以工作。

postgres 9.6.9 版;操作系统 - Debian 9;gcc 版本 6.3.0

标签: c++postgresqlsingletonlibpq

解决方案


推荐阅读