如何解决Mongocxx库分割错误
我正在尝试在Docker映像中安装mongo-c-driver
和mongo-cxx-driver
,但是当我运行C++
程序时,它将引发Segmentation错误。有任何想法吗?瓦尔格朗德说这是蒙哥的错。
我有一些物联网设备,并且在我自己的计算机上,它们运行正常。
我正在使用:
-
ubuntu:focal
作为基本图片 -
mongo-c-driver
版本1.16.2
(也尝试过1.17
和1.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,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,nlohmann::basic_json<std::map,std::vector,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::allocator<unsigned 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&,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::instance
的一个实例和一个实例。此实例必须存在于整个程序中。
因此,您可以将其添加为static
类的MongoDB
成员:
标题
class MongoDB {
//...
private:
static mongocxx::instance inst;
};
偶像
mongocxx::instance MongoDB::inst{};
在此示例中,我连接到默认的mongodb服务器(mongodb://localhost:27017
),使用数据库dbted
和集合movie
,这两个数据库都是先前使用{{1 }} CLI。
我添加了一种方法,可以向集合中的所有文档获取mongo
。
mongocxx::cursor
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。