1
我有一個小程序,試圖連接到mongodb並檢查它會收到多少個對象,如果我希望能夠接收對象。在這種情況下,我甚至無法獲得對象的數量,即使我除了連接之外什麼都不做,我會在清理時進行段錯誤。Mongodb C++驅動程序,在清理失敗
我正在使用GlobalInstance對象來維持我的連接並在退出時清理我。這個函數我從main調用。該程序還沒有做任何其他事情。
關於我在做什麼的錯誤,我segfault?據推測,這與無法調用connection-> count()有關?
非常感謝任何指針。
int foo() {
mongo::client::Options options;
options.setSSLMode(mongo::client::Options::kSSLRequired);
mongo::client::GlobalInstance mongo_connection(options);
mongo::DBClientBase* connection;
if (!mongo_connection.status().isOK()) {
cout << "Mongo connection not established: "
<< mongo_connection.status() << endl;
}
try {
mongo::DBClientConnection c(true);
string error;
ostringstream mongo_url;
mongo_url << "mongodb://" << db_username << ":" << db_password << "@"
<< db_host << ":" << db_port << "/" << db_name;
const mongo::ConnectionString conn_string =
mongo::ConnectionString::parse(mongo_url.str(), error);
if (!conn_string.isValid()) {
cout << "Bad connection string: " << error << endl;
}
// Reality check. Passes.
cout << "user=" << conn_string.getUser() << endl;
cout << "pass=" << conn_string.getPassword() << endl;
cout << "db= " << conn_string.getDatabase() << endl;
const auto servers = conn_string.getServers();
for (const auto& server : servers) {
cout << "serv=" << server << endl;
}
cout << "type=" << conn_string.type() << endl;
// End reality check.
connection = conn_string.connect(error);
cout << "error says: " << error << endl; // Is empty.
cout << "conn=" << connection << endl; // Not zero.
std::cout << "connected ok" << std::endl;
cout << connection->getConnectionId() << endl; // Prints "1".
// This returns an error 13, not authorized, if I included it.
//cout << "count: " << connection->count("focus_groups") << endl;
} catch(const mongo::DBException &e) {
std::cout << "caught " << e.what() << std::endl;
}
cout << "----------------------------------------------------------------------" << endl;
return 0;
}
/*
Note that I can do this in the mongo shell with no problem:
[email protected]:~ $ mongo my_host:27017/my_db --ssl -u my_user -p
MongoDB shell version: 2.6.3
Enter password:
connecting to: my_host:27017/my_db
> db.focus_groups.count()
26
>
*/
我在64位ubuntu 15.04上運行,並使用clang 3.6.0編譯。通過apt-get安裝Boost 1.55。傳統的mongo C++驅動程序,我使用ssl支持從HEAD(861699d116627d63e1c914384a66e4e3ea7c23bc)編譯並安裝。