[CXX-1388] Corrupted output | bsoncxx::to_json Created: 07/Jul/17  Updated: 27/Oct/23  Resolved: 12/Jan/18

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

Type: Bug Priority: Major - P3
Reporter: Guillaume [X] Assignee: Andrew Morrow (Inactive)
Resolution: Works as Designed Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10 Pro (language: French-FR)
Microsoft Visual Studio Community 2017 v. 15.2 Release
Boost 1.64.0


Attachments: File MongoDB.vcxproj     PNG File mongodb.png    
Issue Links:
Related

 Description   

New to MongoDB, I have followed the official instructions (https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/) to install MongoDB C driver then the last version of MongoDB C++ driver on Windows 10 Pro with MVS 15 2017 Win64.

My first project (the offical example code) compiles. However, the output is corrupted. I have explained my issue here: https://groups.google.com/forum/#!topic/mongodb-user/KxN5llw86Vc

The code:

    #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;
        }
    }

The same issue appears with this code:

    #include <iostream>
   
    #include <bsoncxx/builder/basic/document.hpp>
    #include <bsoncxx/json.hpp>
   
    int main(int, char**) {
        bsoncxx::builder::basic::document doc{};
        doc.append(bsoncxx::builder::basic::kvp("test", 1));
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

A Stackoverflow user has a similar issue: https://stackoverflow.com/questions/40309483/bsoncxxto-json-return-corrupted-string/40310414

I have tried different MVS configurations with no success.



 Comments   
Comment by Guillaume [X] [ 12/Jan/18 ]

Hi.

Thank you for your feedback.

I solved my issue with a strictly static library build.

Comment by Andrew Morrow (Inactive) [ 12/Jan/18 ]

I have run both of your examples locally without issue. I can point out two things to consider:

  • In your first example, you should finalize the document before viewing it for your insert.
  • In your second example, you need to create an instance object before using any entry points.

However, neither of these seems to be causative of the sort of problems you are seeing.

I recommend that you do the following:

  • Ensure that your application, the driver, and any other libraries you may use are all built consistently with respect to static vs dynamic runtime, debug vs retail libraries, and static vs dynamic build.
  • Run your program under the Visual Studio facilities for debugging memory corruption, race conditions, or undefined behavior.
Generated at Wed Feb 07 22:02:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.