bson_json Access violation reading location error with mongocxx(v3)

100 views
Skip to first unread message

Gillian Valenzuela

unread,
May 19, 2017, 9:17:45 AM5/19/17
to mongodb-user
Hi,

I have been triying to read documents from a collection on VS 14, but when a I run this code:

 mongocxx::cursor cursor = coll.find({}); for (auto doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << "\n";
    }
I got this error message:

Exception thrown at 0x000000007739EEF1 (ntdll.dll) in test.exe: 0xC0000005: Access violation reading location

Someone has an idea about what I'm doing wrong?

Regards.

Sam Rossi

unread,
May 22, 2017, 12:33:59 PM5/22/17
to mongodb-user
Hi Gillian! Thanks for bringing this to our attention. Could you could give us a self-contained example of code we can run to reproduce this (preferably by manually creating the document to pass to bsoncxx::to_json, or barring that, a document that we can insert into the database to query for)? Additionally, could you detail the polyfill version that you're using? Given that you're on Windows, I expect that you're using Boost.
Message has been deleted

Gillian Valenzuela

unread,
May 24, 2017, 2:53:33 PM5/24/17
to mongodb-user
Hi Sam! Thanks for your help. Happen something different now. When I separate the code, to give you a self-contained example, the code run perfect, and it's this:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <bsoncxx\builder\stream\document.hpp>
#include <bsoncxx\builder\basic\document.hpp>
#include <bsoncxx\json.hpp>
#include <mongocxx\client.hpp>
#include <mongocxx\instance.hpp>
#include <bsoncxx\document\value.hpp>
#include <bsoncxx\document\view.hpp>
#include <cstdint>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <bson.h>

using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
using namespace std;

int main() {
    mongocxx
::instance inst{};
    mongocxx
::client client{ mongocxx::uri{ "mongodb://ip:27017" } };

    mongocxx
::database db = client["client"];
    mongocxx
::collection coll = db["collection"];




    mongocxx
::cursor cursor = coll.find({});

   
for (auto doc : cursor) {
        std
::cout << bsoncxx::to_json(doc) << "\n";
   
}


   
return 1;
}

And, I am using boost.

The difference with the other code, is that it's connected to mysql, and I am trying to change to mongodb. I don't know if there is some relation with the error. Do you have any advice?

Sam Rossi

unread,
May 25, 2017, 12:50:32 PM5/25/17
to mongodb-user
I'm not super familiar with Windows or Visual Studio, but from some quick Googling, it looks like "Access violation" refers to dereferencing a null or invalid pointer. I found this page, which describes how to debug these types of errors. Can you run the original code from within Visual Studio like it suggests and report back the error message it gives in the dialog?
Reply all
Reply to author
Forward
0 new messages