[CXX-1507] test.cpp from documentation page segfaults sometimes Created: 01/Feb/18  Updated: 24/Apr/18  Resolved: 08/Feb/18

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Miha Nedok Assignee: Andrew Morrow (Inactive)
Resolution: Incomplete Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

All steps from online documentation:
https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/
cxx driver installed from git with:
git clone https://github.com/mongodb/mongo-cxx-driver.git \
--branch releases/stable --depth 1

created test.cpp with contents:

#include <iostream>
 
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
 
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
 
int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{}};
 
    bsoncxx::builder::stream::document document{};
 
    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";
 
    collection.insert_one(document.view());
    auto cursor = collection.find({});
 
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }
}

compiled with:

c++ --std=c++11 test.cpp -o test \
    -I/usr/local/include/mongocxx/v_noabi \
    -I/usr/local/include/bsoncxx/v_noabi \
    -L/usr/local/lib -lmongocxx -lbsoncxx

Sometimes crashes.
*Backtrace:

#0  0x00007ffff6f02266 in bson_destroy () from /usr/local/lib/libbson-1.0.so.0
#1  0x00007ffff71739e5 in _mongoc_write_command_destroy () from /usr/local/lib/libmongoc-1.0.so.0
#2  0x00007ffff713d7a7 in mongoc_bulk_operation_destroy () from /usr/local/lib/libmongoc-1.0.so.0
#3  0x00007ffff7b3da71 in mongocxx::v_noabi::bulk_write::~bulk_write() () from /usr/local/lib/libmongocxx.so._noabi
#4  0x00007ffff7b43ba1 in mongocxx::v_noabi::collection::insert_one(bsoncxx::v_noabi::view_or_value<bsoncxx::v_noabi::document::view, bsoncxx::v_noabi::document::value>, mongocxx::v_noabi::options::insert const&) () from /usr/local/lib/libmongocxx.so._noabi
#5  0x0000555555555b7e in main ()
*(gdb)



 Comments   
Comment by Andrew Morrow (Inactive) [ 08/Feb/18 ]

Hi - I'm closing this ticket because I haven't heard any further information. Please feel free to comment on the ticket if there is additional help I can provide with this issue.

Comment by Andrew Morrow (Inactive) [ 01/Feb/18 ]

The only obvious mistake I see in your code is that you haven't finalized your document. Try collection.insert_one(document << finalize);, though I don't exactly see how that error would cause the crash you are seeing. For what it is worth, the stream builders are currently de-emphasized because they are too hard to use correctly. Try using the basic builder instead.

Generated at Wed Feb 07 22:02:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.