Mongocxx 库分段错误
Mongocxx library segmentation fault
我正在尝试在 Docker 映像中安装 mongo-c-driver
和 mongo-cxx-driver
,但是当我 运行 我的 C++
程序时,它会抛出一个分段故障。有任何想法吗? Valgrind 说这是 mongo 的错。
我有一些物联网设备,在我自己的电脑上它们 运行 没问题。
我正在使用:
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, 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
是我的库。
非常感谢,
我不知道是什么原因造成的...
IMPORTANT: Before making any connections, you need to create one and only one instance of mongocxx::instance
. This instance must exist for the entirety of your program.
因此,您可以将此添加为 MongoDB
class 的 static
成员:
header
class MongoDB {
//...
private:
static mongocxx::instance inst;
};
impl
mongocxx::instance MongoDB::inst{};
这是我连接到默认 mongodb 服务器 (mongodb://localhost:27017
) 的示例,使用数据库 dbted
和 collection movie
,这两个都是以前使用 mongo
cli 创建的。
我添加了一个方法来为 collection 中的所有文档获取 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 successfully\n";
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;
}
}
我正在尝试在 Docker 映像中安装 mongo-c-driver
和 mongo-cxx-driver
,但是当我 运行 我的 C++
程序时,它会抛出一个分段故障。有任何想法吗? Valgrind 说这是 mongo 的错。
我有一些物联网设备,在我自己的电脑上它们 运行 没问题。
我正在使用:
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, 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
是我的库。
非常感谢, 我不知道是什么原因造成的...
IMPORTANT: Before making any connections, you need to create one and only one instance of
mongocxx::instance
. This instance must exist for the entirety of your program.
因此,您可以将此添加为 MongoDB
class 的 static
成员:
header
class MongoDB {
//...
private:
static mongocxx::instance inst;
};
impl
mongocxx::instance MongoDB::inst{};
这是我连接到默认 mongodb 服务器 (mongodb://localhost:27017
) 的示例,使用数据库 dbted
和 collection movie
,这两个都是以前使用 mongo
cli 创建的。
我添加了一个方法来为 collection 中的所有文档获取 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 successfully\n";
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;
}
}