[CXX-1224] mongocxx::client constructor fails on Windows 7 Created: 17/Feb/17  Updated: 27/Oct/23  Resolved: 21/Feb/17

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

Type: Bug Priority: Major - P3
Reporter: Christian Mazakas Assignee: David Golden
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7, 32 bit binary, 64 bit OS


Issue Links:
Related
related to CXX-1196 Improve mongocxx::instance documentation Closed
related to CXX-1197 Client and pool initialization should... Backlog

 Description   

Basically, if I try to use the simple mongo client data structure listed in the tutorial, I get a hard crash in Windows 7.

This is my fully-contained working example that demonstrates the issue:
```
#include <cstdint>
#include <iostream>
#include <vector>

#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>

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;

int main(void)
{
try {

mongocxx::uri uri("mongodb://localhost:27017");
// this is what's failing
mongocxx::client client

{ uri }

;
}
catch (std::exception err)

{ std::cout << err.what() << "\n"; }

return 0;
}
```

It seems like it's an issue deep in the Mongo callstack:
> libmongoc-1.0.dll!_InterlockedExchangeAdd64(volatile __int64 * Addend, __int64 Value) Line 5603 C

Namely, this line here:
```
do

{ // this one is what VS picks up on Old = *Addend; }

while (InterlockedCompareExchange64(Addend,
Old + Value,
Old) != Old);
```

I installed the mongo-cxx-driver using vcpkg as well.



 Comments   
Comment by David Golden [ 21/Feb/17 ]

I'm glad you figured it out. As you can see from related tickets, we know this is something we could make more user-friendly.

Comment by Christian Mazakas [ 21/Feb/17 ]

Okay, turns out that I'm a super duper dummy.

All I had to do was revert the expression `mongocxx::instance{}` into `mongocxx::instance instance{}` and the code works beautifully.

Basically, the instance stuff isn't mentioned that much in the tutorials nor is it mentioned what the lifetime of it needs to be.

Ideally the code should throw a useful exception instead of having a segmentation fault which is what it was basically doing for me.

But the MongoDB C++ driver is amazing and I appreciate all the hard work that goes into it.

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