【问题标题】:Mongo C++ driver crashes with bad allocationMongodb C++ 驱动程序因分配错误而崩溃
【发布时间】:2014-08-07 07:54:27
【问题描述】:

我从 https://github.com/mongodb/mongo-cxx-driver/tree/26compat 编译了 c++ 驱动程序(使用 26compat 分支编译驱动程序,使用命令 scons --64 --sharedclient --dynamic-windows --release --full --prefix=C:/mongo-cxx -driver-legacy/mongo-client-release_64 --use-system-boost --cpppath=C:/local/boost_1_55_0_64 --libpath=C:/local/boost_1_55_0_64/lib64-msvc-12.0 install-mongoclient) 注意:安装了 https://github.com/mongodb/mongo-cxx-driver/wiki/Download%20and%20Compile 中提到的所有先决条件,如 boost 包等

我编写了一个示例程序(附加),我刚刚获得了 mongo 连接,连接想要查找 BSON 对象的文档数。 这是程序-

#if defined(_WIN32)
#include <winsock2.h>
#include <windows.h>
#endif
#define _CRT_SECURE_NO_WARNINGS
#define STATIC_LIBMONGOCLIENT
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <mongo/client/dbclient.h> // for the driver
#include <mongo/client/dbclientinterface.h> // for the driver
#include <mongo/bson/bson.h>

#pragma   comment(lib,"libmongoclient.lib")
using namespace mongo;

int main(){

    DBClientConnection mongoclient;

    string errmsg;
    try {

        if (!mongoclient.connect("16.166.233.124:27017", errmsg)) {
            cout << "couldn't connect : " << errmsg << endl; 
        }
        cout << "\nSuresh-Test264  After getting the connection....\n" << endl;;
        const char * ns = "suresh.test";
        BSONObj res = BSONObjBuilder().append("age", 33).obj();
        int limit, skip, options;
        limit = skip = options = 0;
        unsigned long long count = 0;
        count = mongoclient.count(ns, res);
        cout << "count of existing documents in collection test.foo : " << count << endl;
        }
    catch (exception &e) {
        std::cout << "caught " << e.what() << std::endl;
    }


}

我成功获得了连接,但是计数操作抛出了错误的内存分配异常。 [DBClientConnection中定义的大部分函数都会抛出这个异常]

线程 0x33c0 以代码 0 (0x0) 退出。 ConsoleApplication2.exe 中 0x000007FEFD79940D 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x00000000001DF6F0 处的 std::bad_alloc。 ConsoleApplication2.exe 中 0x000007FEFD79940D 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x00000000001DD200 处的 std::bad_alloc。 ConsoleApplication2.exe 中 0x000007FEFD79940D 处的第一次机会异常:Microsoft C++ 异常:[rethrow] 在内存位置 0x0000000000000000。 ConsoleApplication2.exe 中 0x000007FEFD79940D 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x00000000001DD200 处的 std::bad_alloc。 程序“[11696] ConsoleApplication2.exe”已退出,代码为 0 (0x0)。

调试错误信息-

ConsoleApplication2.exe!_heap_alloc_dbg_impl(unsigned __int64 nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) 第 396 行 C++ ConsoleApplication2.exe!_nh_malloc_dbg_impl(unsigned __int64 nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) 第 239 行 C++ ConsoleApplication2.exe!_nh_malloc_dbg(unsigned __int64 nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) 第 302 行 C++ ConsoleApplication2.exe!malloc(unsigned __int64 nSize) 第 56 行 C++ ConsoleApplication2.exe!operator new(unsigned __int64 size) Line 59 C++ ConsoleApplication2.exe!std::_Allocate(unsigned __int64 _Count, std::_Container_proxy * __formal) 第 28 行 C++ ConsoleApplication2.exe!std::allocator::allocate(unsigned __int64 _Count) 第 579 行 C++ ConsoleApplication2.exe!std::_String_alloc >::_Alloc_proxy() 第 666 行 C++ ConsoleApplication2.exe!std::_String_alloc>::_String_alloc>(const std::allocator & __formal) 第 645 行 C++ ConsoleApplication2.exe!std::basic_string,std::allocator >::basic_string,std::allocator >(const char * _Ptr) 第 780 行 C++ ConsoleApplication2.exe!main() 第 33 行 C++ [外部代码]

我的环境包括- Windows 7、64 位。 视觉工作室 2013 MongoDB 2.4.4

【问题讨论】:

    标签: c++ mongodb driver


    【解决方案1】:

    我在 mongodb-user 邮件列表中回答了您的等效问题,如下所示:

    我使用地址清理器在 linux 上的 26compat 分支上运行了您的程序,一切看起来都很好。所以我有几个问题:

    • 您正在使用--sharedclient 构建,但是您的#pragma 命名了库的静态版本。您打算链接静态版还是动态版的 mongoclient?
    • 您正在使用 --dynamic-windows 构建,这将导致 mongo 客户端库选择动态 windows 运行时库。您是否还在针对动态 Windows 运行时库编译您的应用程序?所选运行时库必须在 mongo 客户端库和应用程序之间匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 2012-07-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多