2

I try C++ Driver 3.1.2, the code still crashed in client deconstructor

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

The code crashed in insert part, the rest of it is not yet tested, so I haven't posted in last time, sorry for the inconvenience.

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;
}

The output of ldd is below:

    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

The insert operation always succeeds, I can query what I insert in the database, but the code always crashed in client deconstructor.

Below is the output from 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

Many Thanks!

Best Regards

SPM
  • 405
  • 1
  • 5
  • 16
zed
  • 31
  • 5
  • What version of the C++ driver and C driver are you using? – acm Jan 29 '18 at 16:22
  • My first recommendation would be to run your program under valgrind, or to build it with AddressSanitizer enabled and see if you get any interesting output when you run the instrumented binaries. – acm Jan 29 '18 at 16:24
  • C Driver: 1.9.2 C++ Driver: 3.2.0 – zed Jan 29 '18 at 16:47
  • I notice that you are using C++ driver 3.2.0, which is currently an rc1 state, and not released. Can you let me know the exact git hash or tag that you are using? Also, can you see if you get the same problem if you downgrade to C++ driver 3.1.3? – acm Jan 30 '18 at 15:56
  • Also, can you please include the rest of the code? You have this almost at an compilable state. If you include the header for MongoMgr and a simple mainline that calls it and exhibits the problem, then I can test locally without trying to re-create the missing parts of your code. – acm Jan 30 '18 at 16:04
  • The complete MongoMgr is posted, sorry for any inconvenience. – zed Jan 30 '18 at 17:12
  • I put together your code locally (I had to fake out the `m_confLib` bit, and it does not crash for me. Now, that doesn't mean anything! There could still be a bug in the driver. – acm Jan 30 '18 at 20:13
  • Can you let me know which version of C Driver, C++ Driver and boost you are using? At least I can avoid this error, otherwise, I have to implement my own connection pool. Thanks. – zed Jan 31 '18 at 04:44
  • I'd prefer we got to the bottom of this issue, if possible. Because there is either a serious but subtle bug in your code, and it would be unwise to proceed further in that condition, or there is a subtle bug in the driver code which must be figured out. I'm using C driver 1.9.2, C++ driver master, and boost 1.66. – acm Jan 31 '18 at 13:30
  • Can you provide the CMake and build system invocation you used to build the driver, and the compile line for your application? Also, the output of ldd on your application binary would be helpful. This issue is starting to feel to me like a mismatch between library components, somehow. – acm Jan 31 '18 at 13:33
  • CMake 3.7.1, gcc 7.2.1, CentOS 7.2, boost 1.5.3, output of ldd is posted. – zed Jan 31 '18 at 15:52
  • How did you invoke CMake to configure the C++ driver build? How did you invoke the generated build system? – acm Jan 31 '18 at 16:22
  • Thank you very much, @acm. I finally solved it. I got two way to fix this problem. The first solution is implemented my own connection pool and manage the connections like thread pool. The second one is compiling the boost again, because I saw your answer to other questioners, boost compiler should be same as my code, but my original boost installed by `yum`. I think that is the reason why my code crashed. Thanks again. – zed Feb 01 '18 at 16:56

1 Answers1

0

This line looks suspicious to me: m_ptrPool = boost::shared_ptr<mongocxx::pool>{uri};

Can you change that to m_ptrPool = boost::make_shared<mongocxx::pool>(uri);?

acm
  • 12,183
  • 5
  • 39
  • 68
  • Thank you for reply, this line originally is m_ptrPool = boost::shared_ptr(new mongocxx::pool{*m_ptrDbUri}); I did that only for attach the uri. – zed Jan 29 '18 at 16:43
  • @zed please update the code in your question to reflect what you are actually running when you see the crash, or it is very difficult to help. Also, did you try valgrind or ASAN? – acm Jan 29 '18 at 22:30
  • Sorry for confusing you, the code is updated. When I run it under valgrind, the output mostly like gdb,`'Invalid read of size 8 at 0x5481926: mongocxx::v_noabi::client::~client()` . and `Process terminating with default action of signal 11 (SIGSEGV): dumping core` . no memory leak. Should I attacth all of them to you? – zed Jan 30 '18 at 03:21
  • Yeah, try running valgrind with `--track-origins=yes` so we see where that was allocated from, if at all, and then post the output in your question. – acm Jan 30 '18 at 14:42
  • Hi, @acm, the output of Valgrind is posted. Thank you. – zed Jan 30 '18 at 15:29
  • The solution is compile the boost and program with the same compiler. – zed Feb 01 '18 at 16:58