MongoDB c++旧驱动1.1.2不工作-与VS2013, win10, boost-1_62编译

MongoDB c++ legacy driver 1.1.2 not working - Compiled with VS2013, Win 10, boost-1_62

本文关键字:win10 VS2013 boost-1 编译 工作 c++ MongoDB      更新时间:2023-10-16

驱动编译成功,没有问题。我编写了一个简单的程序来测试驱动程序。基本代码如下:

static std::vector<mongo::HostAndPort> hosts = { mongo::HostAndPort("xxxxxxxx-a0.mlab.com:xxxxx"), mongo::HostAndPort("xxxxxxxx-a1.mlab.com:xxxxx") };
static mongo::DBClientReplicaSet con("rs-xxxxxxxx", hosts, 0);

std::string errmsg;
mongo::client::initialize();
con.connect();
con.auth("dbname", "userid", "password", errmsg);

我编译代码没有问题。exe无法在bug模式下启动,给出错误0xc000a200。我根本无法调试。在释放模式下,它在启动时就停止了。我注意到控制台有一个boost警告:

Assertion failed: px != 0, file C:Boostincludeboost-1_62boost/smart_ptr/scoped_ptr.hpp, line 105

弹出错误提示:

MongoDB c++ Legacy Driver Error

我可以选择调试,这里是程序停止的地方- replica_set_monitor.cpp的最后一行:

void ReplicaSetMonitor::createIfNeeded(const string& name, const set<HostAndPort>& servers) {
    LOG(3) << "ReplicaSetMonitor::createIfNeeded " << name;
    boost::lock_guard<boost::mutex> lk(setsLock);
    ReplicaSetMonitorPtr& m = sets[name];
    if (!m)
        m = boost::make_shared<ReplicaSetMonitor>(name, servers);
    // Don't need to hold the lifetime lock for safeGo as
    // 1) we assume the monitor is created as the contract of this class is such that initialize()
    //    must have been called.
    // 2) replicaSetMonitorWatcher synchronizes safeGo internally using the _monitorMutex
    replicaSetMonitorWatcher->safeGo();
}

请帮忙!非常感谢!

您需要在构建任何驱动程序对象或BSON之前调用mongo::client::initialize 。将调用移动到上面声明DBClientReplicaSet对象的mongo::client::initialize