mongocxx 在 ~client() 中崩溃

mongocxx crashed in ~client()

我尝试了C++ Driver 3.1.2,代码在客户端解构器中仍然崩溃

MongoMgr.h

#ifndef _MOMGODB_MANAGER_H_
#define _MOMGODB_MANAGER_H_

#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/pool.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/exception/exception.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
#include <vector>

#include "ConfFile.h"


using namespace std;


using bsoncxx::builder::basic::kvp;
//using bsoncxx::builder::basic;
//using bsoncxx::builder::basic::make_array;
//using bsoncxx::builder::basic::make_document;

class MongoMgr : boost::noncopyable
{
    using string = std::string;

public:
    // Constructor function
    MongoMgr(string path = "/home/dev/memo_app/server/config_file/MongoConfig.json") 
        : m_confLib(path)
    {
        //setPoolConfig();
    }

    // Deconstructor
    // Free all resources
    ~MongoMgr();

    // Manager Initialization
    int init();

    // Insert single document to database
    bool insertSingleDoc(string db, string collect, string doc);

    // Insert multiple documents to database
    bool insertMultipleDoc(string db, string collect, vector<string> docs);

    // Query Documents
    string queryDoc(string db, string collect, string conditions);

    // Update Documents
    // Only update top-level fields in a single document.
    // @Conditions should contains unique key
    bool updateSingleDoc(string db, string collect, string values, string conditions);

    // Update Multiple Documents
    bool updateMultipleDoc(string db, string collect, string values, string conditions);

    // Replace Documents
    // Only replace top-level fields in a single document.
    // @Conditions should contains unique key
    bool replaceDoc(string db, string collect, string values, string conditions);

    // Remove Document
    // @isRemoveOne, true stand for remove one document, false stand for remove multiple
    bool deleteDoc(string db, string collect, string conditions, bool isDeleteOne = true);

    // Get a connection (client) from pool
    using connection = mongocxx::pool::entry;
    connection getConnection() { return m_ptrPool->acquire(); }

protected:
    //set pool configuration before initialize it
    void setPoolConfig();

private:
    // Mongodb Instance
    typedef boost::shared_ptr<mongocxx::instance> mongoInstPtr;
    mongoInstPtr m_ptrDbInst;

    // Mongodb pool infomation
    typedef boost::shared_ptr<mongocxx::uri> mongoUriPtr;
    mongoUriPtr m_ptrDbUri;

    // Mongodb pool
    typedef boost::shared_ptr<mongocxx::pool> mongoPoolPtr;
    mongoPoolPtr m_ptrPool;

    // Configure file
    ConfFile m_confLib;

};

// Glabal variable MongoDB Manager
extern boost::shared_ptr<MongoMgr> g_pMongoMgr;

#endif

插入部分代码崩溃,其余部分尚未测试,所以我上次没有发布,不便之处,敬请谅解。

MongoMgr.cpp

#include "MongoMgr.h"
#include <iostream>

MongoMgr::~MongoMgr()
{

}

int MongoMgr::init()
{
    try
    {
        m_ptrDbInst = boost::shared_ptr<mongocxx::instance>(new mongocxx::instance{});
        setPoolConfig();
        m_ptrPool = boost::shared_ptr<mongocxx::pool>(new mongocxx::pool{*m_ptrDbUri});
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
        exit(0);
    }
}

bool MongoMgr::insertSingleDoc(string db, string collect, string doc)
{
    bool bRet;
    try
    {
        auto client = m_ptrPool->acquire();
        auto collection = (*client)[db.c_str()][collect];

        auto docContent = bsoncxx::from_json(doc);
        collection.insert_one(docContent.view());
        bRet = true;
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
        bRet = false;
    }

    return bRet;
}

bool MongoMgr::insertMultipleDoc(string db, string collect, vector<string> docs)
{
    bool bRet;
    try
    {
        auto client = m_ptrPool->acquire();
        auto collection = (*client)[db][collect];
        vector<bsoncxx::document::value> vecDocs;
        for (auto strDoc : docs)
        {
            vecDocs.push_back(bsoncxx::from_json(strDoc));
        }
        collection.insert_many(vecDocs);
        bRet = true;
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
        bRet = false;
    }

    return bRet;
}

string MongoMgr::queryDoc(string db, string collect, string conditions)
{
    string ret;
    try 
    {
        auto client = m_ptrPool->acquire();
        auto collection = (*client)[db][collect];
        auto cursor = collection.find(bsoncxx::from_json(conditions));

        if (cursor.begin() == cursor.end())
        {
            return string();
        }

        ret = "[";
        for (auto&& doc : cursor) {
            ret += bsoncxx::to_json(doc);
        }
        ret += "]";
        //if (0 == ret.compare("[]")) ret.clear();
        return ret;
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
        ret = "Error";
    }
    return ret;

}

bool MongoMgr::updateSingleDoc(string db, string collect, string values, string conditions)
{
    bool bRet;
    try
    {
        auto client = m_ptrPool->acquire();
        auto collection = (*client)[db][collect];
        auto docConditions = bsoncxx::from_json(conditions);
        auto docValues = bsoncxx::from_json(values);

        collection.update_one(docConditions.view(), docValues.view());
        bRet = true;
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
        bRet = false;
    }

    return bRet;
}

bool MongoMgr::updateMultipleDoc(string db, string collect, string values, string conditions)
{
    bool bRet;
    try
    {
        auto client = m_ptrPool->acquire();
        auto collection = (*client)[db][collect];
        auto docConditions = bsoncxx::from_json(conditions);
        auto docValues = bsoncxx::from_json(values);

        collection.update_many(docConditions.view(), docValues.view());
        bRet = true;
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
        bRet = false;
    }

    return bRet;
}

bool MongoMgr::replaceDoc(string db, string collect, string values, string conditions)
{
    bool bRet;
    try
    {
        auto client = m_ptrPool->acquire();
        auto collection = (*client)[db][collect];
        auto docConditions = bsoncxx::from_json(conditions);
        auto docValues = bsoncxx::from_json(values);

        collection.replace_one(docConditions.view(), docValues.view());
        bRet = true;
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
        bRet = false;
    }

    return bRet;
}

bool MongoMgr::deleteDoc(string db, string collect, string conditions, bool isDeleteOne)
{
    bool bRet;
    try
    {
        auto client = m_ptrPool->acquire();
        auto collection = (*client)[db][collect];
        auto docConditions = bsoncxx::from_json(conditions);

        if (true == isDeleteOne)
        {
            collection.delete_one(docConditions.view());
        }
        else if (false == isDeleteOne)
        {
            collection.delete_many(docConditions.view());
        }
        bRet = true;
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
        bRet = false;
    }

    return bRet;
}

void MongoMgr::setPoolConfig()
{
    string strConfig = "mongodb://";
    strConfig += m_confLib["db_ip"].get<string>() + string(":");
    strConfig += m_confLib["db_port"].get<string>() + string("/?");
    strConfig += string("minPoolSize=") + m_confLib["minPoolSize"].get<string>() + string("&");
    strConfig += string("maxPoolSize=") + m_confLib["maxPoolSize"].get<string>();
    try
    {
        m_ptrDbUri = boost::shared_ptr<mongocxx::uri>(new mongocxx::uri{strConfig});
    }
    catch (const mongocxx::exception & e)
    {
        std::cout << e.what() << std::endl;
    }
}

main.cpp

#include <boost/shared_ptr.hpp>
#include <string>
#include "MongoMgr.h"
#include <iostream>

using mongoMgrPtr = boost::shared_ptr<MongoMgr>;

// MongoDB manager global pointer
mongoMgrPtr g_pMongoMgr;

#define DB_USED     "memotest"
#define COLLECTION  "mt"

int main(int argc, char* argv[])
{
    // MongoDB manager initailize
    g_pMongoMgr = mongoMgrPtr(new MongoMgr());
    g_pMongoMgr->init();

    string doc = "{\"name\":\"Zed\"}";
    bool bInsert = g_pMongoMgr->insertSingleDoc(DB_USED, COLLECTION, doc);
    if (true == bInsert) cout << "Insert document successed!" << endl;

    //string strQuery = g_pMongoMgr->queryDoc(DB_USED, COLLECTION, jMongoTest.dump());
    //cout << strQuery << endl;

    return 0;
}

ldd 的输出如下:

    linux-vdso.so.1 =>  (0x00007fffa5b92000)
    libuuid.so.1 => /lib64/libuuid.so.1 (0x00007f43c7a7a000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f43c785d000)
    librt.so.1 => /lib64/librt.so.1 (0x00007f43c7655000)
    libmongocxx.so._noabi => /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi (0x00007f43c7401000)
    libbsoncxx.so._noabi => /home/dev/memo_app/dependencies/lib/libbsoncxx.so._noabi (0x00007f43c71e6000)
    libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f43c6ede000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f43c6bdc000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f43c69c5000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f43c6602000)
    /lib64/ld-linux-x86-64.so.2 (0x000055d327097000)
    libmongoc-1.0.so.0 => /usr/local/lib/libmongoc-1.0.so.0 (0x00007f43c6394000)
    libssl.so.10 => /lib64/libssl.so.10 (0x00007f43c6121000)
    libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007f43c5cc0000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f43c5aa6000)
    libz.so.1 => /lib64/libz.so.1 (0x00007f43c588f000)
    libbson-1.0.so.0 => /usr/local/lib/libbson-1.0.so.0 (0x00007f43c565a000)
    libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007f43c540c000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007f43c5124000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007f43c4f20000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007f43c4cec000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f43c4ae8000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007f43c48da000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007f43c46d5000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f43c44ae000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f43c424b000)

    Version information:
    ./memoSvr:
            libgcc_s.so.1 (GCC_3.0) => /lib64/libgcc_s.so.1
            libstdc++.so.6 (GLIBCXX_3.4.13) => /lib64/libstdc++.so.6
            libstdc++.so.6 (CXXABI_1.3) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4.14) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4.11) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4.15) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4.5) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4) => /lib64/libstdc++.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libpthread.so.0 (GLIBC_2.2.5) => /lib64/libpthread.so.0
    /lib64/libuuid.so.1:
            ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /lib64/libpthread.so.0:
            ld-linux-x86-64.so.2 (GLIBC_2.2.5) => /lib64/ld-linux-x86-64.so.2
            ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
            ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.2) => /lib64/libc.so.6
            libc.so.6 (GLIBC_PRIVATE) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /lib64/librt.so.1:
            libpthread.so.0 (GLIBC_2.3.2) => /lib64/libpthread.so.0
            libpthread.so.0 (GLIBC_PRIVATE) => /lib64/libpthread.so.0
            libpthread.so.0 (GLIBC_2.2.5) => /lib64/libpthread.so.0
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.2) => /lib64/libc.so.6
            libc.so.6 (GLIBC_PRIVATE) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi:
            libgcc_s.so.1 (GCC_3.0) => /lib64/libgcc_s.so.1
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libstdc++.so.6 (CXXABI_1.3) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4.11) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4.15) => /lib64/libstdc++.so.6
    /home/dev/memo_app/dependencies/lib/libbsoncxx.so._noabi:
            libgcc_s.so.1 (GCC_3.0) => /lib64/libgcc_s.so.1
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libstdc++.so.6 (CXXABI_1.3) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4.15) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4) => /lib64/libstdc++.so.6
            libstdc++.so.6 (GLIBCXX_3.4.11) => /lib64/libstdc++.so.6
    /lib64/libstdc++.so.6:
            ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
            libm.so.6 (GLIBC_2.2.5) => /lib64/libm.so.6
            libgcc_s.so.1 (GCC_4.2.0) => /lib64/libgcc_s.so.1
            libgcc_s.so.1 (GCC_3.3) => /lib64/libgcc_s.so.1
            libgcc_s.so.1 (GCC_3.0) => /lib64/libgcc_s.so.1
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.2) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /lib64/libm.so.6:
            ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libc.so.6 (GLIBC_PRIVATE) => /lib64/libc.so.6
    /lib64/libgcc_s.so.1:
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /lib64/libc.so.6:
            ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
            ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2
    /usr/local/lib/libmongoc-1.0.so.0:
            libz.so.1 (ZLIB_1.2.0) => /lib64/libz.so.1
            librt.so.1 (GLIBC_2.2.5) => /lib64/librt.so.1
            libresolv.so.2 (GLIBC_2.2.5) => /lib64/libresolv.so.2
            libresolv.so.2 (GLIBC_2.9) => /lib64/libresolv.so.2
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.6) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libpthread.so.0 (GLIBC_2.2.5) => /lib64/libpthread.so.0
            libpthread.so.0 (GLIBC_2.3.2) => /lib64/libpthread.so.0
            libssl.so.10 (libssl.so.10) => /lib64/libssl.so.10
            libcrypto.so.10 (libcrypto.so.10) => /lib64/libcrypto.so.10
    /lib64/libssl.so.10:
            libk5crypto.so.3 (k5crypto_3_MIT) => /lib64/libk5crypto.so.3
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.17) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libkrb5.so.3 (krb5_3_MIT) => /lib64/libkrb5.so.3
            libcrypto.so.10 (OPENSSL_1.0.1_EC) => /lib64/libcrypto.so.10
            libcrypto.so.10 (libcrypto.so.10) => /lib64/libcrypto.so.10
    /lib64/libcrypto.so.10:
            libdl.so.2 (GLIBC_2.2.5) => /lib64/libdl.so.2
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.7) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.17) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
    /lib64/libresolv.so.2:
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_PRIVATE) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
    /lib64/libz.so.1:
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /usr/local/lib/libbson-1.0.so.0:
            libpthread.so.0 (GLIBC_2.2.5) => /lib64/libpthread.so.0
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.7) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.17) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /lib64/libgssapi_krb5.so.2:
            libk5crypto.so.3 (k5crypto_3_MIT) => /lib64/libk5crypto.so.3
            libkrb5support.so.0 (krb5support_0_MIT) => /lib64/libkrb5support.so.0
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.8) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libkrb5.so.3 (krb5_3_MIT) => /lib64/libkrb5.so.3
    /lib64/libkrb5.so.3:
            libresolv.so.2 (GLIBC_2.2.5) => /lib64/libresolv.so.2
            libk5crypto.so.3 (k5crypto_3_MIT) => /lib64/libk5crypto.so.3
            libkrb5support.so.0 (krb5support_0_MIT) => /lib64/libkrb5support.so.0
            libkeyutils.so.1 (KEYUTILS_1.0) => /lib64/libkeyutils.so.1
            libkeyutils.so.1 (KEYUTILS_1.5) => /lib64/libkeyutils.so.1
            libkeyutils.so.1 (KEYUTILS_0.3) => /lib64/libkeyutils.so.1
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.8) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.16) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
    /lib64/libcom_err.so.2:
            ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
            libpthread.so.0 (GLIBC_2.2.5) => /lib64/libpthread.so.0
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.17) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /lib64/libk5crypto.so.3:
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libkrb5support.so.0 (krb5support_0_MIT) => /lib64/libkrb5support.so.0
    /lib64/libdl.so.2:
            ld-linux-x86-64.so.2 (GLIBC_PRIVATE) => /lib64/ld-linux-x86-64.so.2
            libc.so.6 (GLIBC_PRIVATE) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /lib64/libkrb5support.so.0:
            libdl.so.2 (GLIBC_2.2.5) => /lib64/libdl.so.2
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.8) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.7) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
    /lib64/libkeyutils.so.1:
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.7) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
    /lib64/libselinux.so.1:
            libdl.so.2 (GLIBC_2.2.5) => /lib64/libdl.so.2
            ld-linux-x86-64.so.2 (GLIBC_2.3) => /lib64/ld-linux-x86-64.so.2
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.8) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.7) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
    /lib64/libpcre.so.1:
            libpthread.so.0 (GLIBC_2.2.5) => /lib64/libpthread.so.0
            libc.so.6 (GLIBC_2.14) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.4) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.2.5) => /lib64/libc.so.6
            libc.so.6 (GLIBC_2.3) => /lib64/libc.so.6

插入操作总是成功,我可以查询我在数据库中插入的内容,但是代码总是在客户端解构器中崩溃。

下面是 valgrind --tool=memcheck --track-origins=yes ./mongotest

的输出
==2787== Invalid read of size 8
==2787==    at 0x5481926: mongocxx::v_noabi::client::~client() (in /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi)
==2787==    by 0x54B8B81: std::_Function_handler<void (mongocxx::v_noabi::client*), mongocxx::v_noabi::pool::acquire()::{lambda(mongocxx::v_noabi::client*)#1}>::_M_invoke(std::_Any_data const&, mongocxx::v_noabi::client*) (in /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi)
==2787==    by 0x41489A: std::function<void (mongocxx::v_noabi::client*)>::operator()(mongocxx::v_noabi::client*) const (std_function.h:706)
==2787==    by 0x413EDA: std::unique_ptr<mongocxx::v_noabi::client, std::function<void (mongocxx::v_noabi::client*)> >::~unique_ptr() (unique_ptr.h:268)
==2787==    by 0x410A3E: MongoMgr::insertSingleDoc(std::string, std::string, std::string) (MongoMgr.cpp:34)
==2787==    by 0x407690: main (main.cpp:24)
==2787==  Address 0x88f5b78 is 0 bytes after a block of size 8 alloc'd
==2787==    at 0x4C2A203: operator new(unsigned long) (vg_replace_malloc.c:334)
==2787==    by 0x54B8DA8: mongocxx::v_noabi::pool::acquire() (in /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi)
==2787==    by 0x410899: MongoMgr::insertSingleDoc(std::string, std::string, std::string) (MongoMgr.cpp:34)
==2787==    by 0x407690: main (main.cpp:24)
==2787== 
==2787== Invalid read of size 1
==2787==    at 0x5481950: mongocxx::v_noabi::client::~client() (in /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi)
==2787==    by 0x54B8B81: std::_Function_handler<void (mongocxx::v_noabi::client*), mongocxx::v_noabi::pool::acquire()::{lambda(mongocxx::v_noabi::client*)#1}>::_M_invoke(std::_Any_data const&, mongocxx::v_noabi::client*) (in /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi)
==2787==    by 0x41489A: std::function<void (mongocxx::v_noabi::client*)>::operator()(mongocxx::v_noabi::client*) const (std_function.h:706)
==2787==    by 0x413EDA: std::unique_ptr<mongocxx::v_noabi::client, std::function<void (mongocxx::v_noabi::client*)> >::~unique_ptr() (unique_ptr.h:268)
==2787==    by 0x410A3E: MongoMgr::insertSingleDoc(std::string, std::string, std::string) (MongoMgr.cpp:34)
==2787==    by 0x407690: main (main.cpp:24)
==2787==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
==2787== 
==2787== 
==2787== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==2787==  Access not within mapped region at address 0x18
==2787==    at 0x5481950: mongocxx::v_noabi::client::~client() (in /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi)
==2787==    by 0x54B8B81: std::_Function_handler<void (mongocxx::v_noabi::client*), mongocxx::v_noabi::pool::acquire()::{lambda(mongocxx::v_noabi::client*)#1}>::_M_invoke(std::_Any_data const&, mongocxx::v_noabi::client*) (in /home/dev/memo_app/dependencies/lib/libmongocxx.so._noabi)
==2787==    by 0x41489A: std::function<void (mongocxx::v_noabi::client*)>::operator()(mongocxx::v_noabi::client*) const (std_function.h:706)
==2787==    by 0x413EDA: std::unique_ptr<mongocxx::v_noabi::client, std::function<void (mongocxx::v_noabi::client*)> >::~unique_ptr() (unique_ptr.h:268)
==2787==    by 0x410A3E: MongoMgr::insertSingleDoc(std::string, std::string, std::string) (MongoMgr.cpp:34)
==2787==    by 0x407690: main (main.cpp:24)
==2787==  If you believe this happened as a result of a stack
==2787==  overflow in your program's main thread (unlikely but
==2787==  possible), you can try to increase the size of the
==2787==  main thread stack using the --main-stacksize= flag.
==2787==  The main thread stack size used in this run was 8388608.
==2787== 
==2787== HEAP SUMMARY:
==2787==     in use at exit: 130,153 bytes in 3,535 blocks
==2787==   total heap usage: 3,882 allocs, 347 frees, 286,497 bytes allocated
==2787== 
==2787== LEAK SUMMARY:
==2787==    definitely lost: 0 bytes in 0 blocks
==2787==    indirectly lost: 0 bytes in 0 blocks
==2787==      possibly lost: 640 bytes in 1 blocks
==2787==    still reachable: 129,513 bytes in 3,534 blocks
==2787==                       of which reachable via heuristic:
==2787==                         stdstring          : 1,384 bytes in 40 blocks
==2787==         suppressed: 0 bytes in 0 blocks
==2787== Rerun with --leak-check=full to see details of leaked memory
==2787== 
==2787== For counts of detected and suppressed errors, rerun with: -v
==2787== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault

非常感谢!

此致

我觉得这一行很可疑:m_ptrPool = boost::shared_ptr<mongocxx::pool>{uri};

你能把它改成 m_ptrPool = boost::make_shared<mongocxx::pool>(uri); 吗?