Mongo c++驱动程序因分配不当而崩溃

Mongo C++ driver crashes with bad allocation

本文关键字:崩溃 分配 c++ 驱动程序 Mongo      更新时间:2023-10-16

我从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- monclient编译驱动程序)注意:安装了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++异常:std::bad_alloc在内存位置0x00000000001DF6F0。在ConsoleApplication2.exe中的0x000007FEFD79940D的第一次机会异常:Microsoft c++异常:std::bad_alloc在内存位置0x00000000001DD200。在ConsoleApplication2.exe中的0x000007FEFD79940D的第一次机会异常:Microsoft c++异常:[rethrow]在内存位置0x0000000000000000。在ConsoleApplication2.exe中的0x000007FEFD79940D的第一次机会异常:Microsoft c++异常:std::bad_alloc在内存位置0x00000000001DD200。程序'[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 nSizeConsoleApplication2.exe !operator new(unsigned __int64 sizeConsoleApplication2.exe !std::_Allocate(unsigned __int64 _Count, std::_Container_proxy * __formal)第28行ConsoleApplication2.exe !std::allocator::allocate(unsigned __int64 _Count) Line 579 c++std::_String_alloc<0,std::_String_base_types>>::_Alloc_proxy()第666行c++std::_String_alloc<0,std::_String_base_types>>::_String_alloc<0,std::_String_base_types>>(const std::allocator &__formal)第645行ConsoleApplication2.exe !std::basic_string,std::allocator>::basic_string,std::allocator>(const char * _Ptr) Line 780 c++main()第33行c++[外部代码]

我的环境包括-Windows 7, 64位。Visual studio 2013mongodb 2.4.4

我在mongodb-user邮件列表中回答了你的相同问题:

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

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