Mongocxx库分段错误



我正试图在Docker映像中安装mongo-c-drivermongo-cxx-driver,但当我运行C++程序时,它会引发Segmentation错误。有什么想法吗?Valgrind说这是mongo的错。

我有一些物联网设备,在我自己的电脑里运行得很好。

我正在使用:

  • ubuntu:focal作为基础图像
  • mongo-c-driver版本1.16.2(也尝试使用1.171.15(
  • mongo-cxx-driver版本3.5(也尝试使用3.6(
  • MongoDB服务器版本4.2.8
  • g++版本(Ubuntu 9.3.0-10ubuntu2) 9.3.0

代码:

std::string MongoDB::insertDocument(std::string database,
std::string collection, bsoncxx::document::view_or_value document)
{
try
{
mongocxx::uri uri(_connection_string);
mongocxx::client conn(uri); // seg fault occurs here
mongocxx::database db = conn[database];
mongocxx::collection coll = db[collection];
auto inserted = coll.insert_one(document.view());
bsoncxx::oid oid = inserted->inserted_id().get_oid().value;
return oid.to_string();
}
catch (const mongocxx::exception & e)
{
std::cerr << "WARNING: Unable to connect to MongoDB" << std::endl;
return "UNKNOWN";
}
}

可变值:

_connection_string = "mongodb://burns"
database = "hopping"
collection = "executions"
document = 
{
"created_at" : "2020.09.29 10:48:51",
"hostname" : "XXXXX",
"interface" : "wlp3s0mon",
"schedule" : [ 1 ],
"slot_size" : 200,
"type" : "MOCK_NODE"
}

日志:

==213== Invalid read of size 8
==213==    at 0x5695C0A: mongoc_counter_clients_active_add (mongoc-counters.defs:37)
==213==    by 0x5695C0A: mongoc_counter_clients_active_inc (mongoc-counters.defs:37)
==213==    by 0x5695C0A: _mongoc_client_new_from_uri (mongoc-client.c:1101)
==213==    by 0x5318074: mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&) (in /usr/local/lib/libmongocxx.so.3.5.0)
==213==    by 0x4B691D0: MongoDB::insertDocument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bsoncxx::v_noabi::view_or_value<bsoncxx::v_noabi::document::view, bsoncxx::v_noabi::document::value>) (in /usr/local/lib/influx_logging/libinflux_logging.so)
==213==    by 0x4B69714: MongoDB::insertJSONDocument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >) (in /usr/local/lib/influx_logging/libinflux_logging.so)
==213==    by 0x4B61AD0: DataLogger::setExecutionParameters(nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (in /usr/local/lib/influx_logging/libinflux_logging.so)
==213==    by 0x1C232B: main (in /root/mock_node/build/mock_node)
==213==  Address 0x38 is not stack'd, malloc'd or (recently) free'd
==213== 
==213== 
==213== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==213==  Access not within mapped region at address 0x38
==213==    at 0x5695C0A: mongoc_counter_clients_active_add (mongoc-counters.defs:37)
==213==    by 0x5695C0A: mongoc_counter_clients_active_inc (mongoc-counters.defs:37)
==213==    by 0x5695C0A: _mongoc_client_new_from_uri (mongoc-client.c:1101)
==213==    by 0x5318074: mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&) (in /usr/local/lib/libmongocxx.so.3.5.0)
==213==    by 0x4B691D0: MongoDB::insertDocument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bsoncxx::v_noabi::view_or_value<bsoncxx::v_noabi::document::view, bsoncxx::v_noabi::document::value>) (in /usr/local/lib/influx_logging/libinflux_logging.so)
==213==    by 0x4B69714: MongoDB::insertJSONDocument(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >) (in /usr/local/lib/influx_logging/libinflux_logging.so)
==213==    by 0x4B61AD0: DataLogger::setExecutionParameters(nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer, std::vector<unsigned char, std::allocator<unsigned char> > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) (in /usr/local/lib/influx_logging/libinflux_logging.so)
==213==    by 0x1C232B: main (in /root/mock_node/build/mock_node)
==213==  If you believe this happened as a result of a stack
==213==  overflow in your program's main thread (unlikely but
==213==  possible), you can try to increase the size of the
==213==  main thread stack using the --main-stacksize= flag.
==213==  The main thread stack size used in this run was 8388608.
==213== 
==213== HEAP SUMMARY:
==213==     in use at exit: 123,339 bytes in 262 blocks
==213==   total heap usage: 3,719 allocs, 3,457 frees, 367,254 bytes allocated
==213== 
==213== LEAK SUMMARY:
==213==    definitely lost: 400 bytes in 1 blocks
==213==    indirectly lost: 6,752 bytes in 20 blocks
==213==      possibly lost: 576 bytes in 2 blocks
==213==    still reachable: 115,611 bytes in 239 blocks
==213==         suppressed: 0 bytes in 0 blocks
==213== Rerun with --leak-check=full to see details of leaked memory
==213== 
==213== For lists of detected and suppressed errors, rerun with: -s
==213== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

Ps:mock_node是我的可执行文件,influxdb-logging是我的一个库。

非常感谢,我不知道是什么原因造成的。。。

来自mongocxx:教程

重要信息:在进行任何连接之前,您需要创建一个且仅一个mongocxx::instance实例。此实例必须存在于整个程序中。

因此,您可以将其添加为MongoDB类的static成员:

标题

class MongoDB {
//...
private:
static mongocxx::instance inst;
};

impl

mongocxx::instance MongoDB::inst{};

这里有一个示例,我连接到默认的mongodb服务器(mongodb://localhost:27017(,使用数据库dbted和集合movie,这两个数据库都是以前使用mongocli创建的。

我添加了一个方法来为集合中的所有文档获取mongocxx::cursor

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/exception/exception.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <iostream>
class MongoDB {
public:
MongoDB() :
client{mongocxx::uri{"mongodb://localhost:27017"}},
db{client["dbted"]},
coll{db["movie"]} 
{}
mongocxx::cursor all() { return coll.find({}); }
private
mongocxx::client client;
mongocxx::database db;
mongocxx::collection coll;
static mongocxx::instance inst;
};
mongocxx::instance MongoDB::inst{};
int main() {
try {
MongoDB db;
std::cout << "Connected successfullyn";
for(auto doc : db.all()) {
std::cout << bsoncxx::to_json(doc) << 'n';
}
} catch(const std::exception& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
return 1;
}
}

最新更新