Mongo c++驱动程序因分配不当而崩溃
Mongo C++ driver crashes with bad allocation
我从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客户端库和应用程序之间匹配。
- c++11:在VS2015中分配全局STL "map<string, string>" initializer_list崩溃
- 分配内存并在回调时调用C++的 Rust 函数崩溃
- STD :: FSTREAM在使用预先分配的内存时在Main之后崩溃
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 在崩溃时释放分配的指针的正确方法
- PCL发布构建崩溃而无法分配
- 当我释放由CFFI生成的DLL分配的char*时,为什么我的应用程序会崩溃
- 为什么在复制构造函数中分配联合成员会崩溃
- 在构造函数[C ]中的FP分配上崩溃
- 矢量未正确擦除内容(复制分配运算符的量运行直到崩溃 [BEX])
- 程序在解除分配字符数组时崩溃
- 删除在不同函数中动态分配的对象时崩溃
- 如果在进程崩溃后在进程中分配内存,会发生什么情况
- CComPtrBase::~解除分配智能指针时,CComPtr Base崩溃
- 删除矢量元素指向的已分配内存会导致程序崩溃
- 矩阵分配导致崩溃
- 释放动态分配的内存时程序崩溃
- Boost.SSpirit.Qi在将规则分配给包括自身在内的序列时崩溃
- QString分配崩溃
- 程序崩溃(动态内存分配)