[CXX-637] OID.init() exception when MongoDB Driver is used in DLL Created: 09/Jul/15  Updated: 06/Dec/16  Resolved: 09/Jul/15

Status: Closed
Project: C++ Driver
Component/s: API
Affects Version/s: legacy-1.0.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Thomas M. [X] Assignee: Andrew Morrow (Inactive)
Resolution: Done Votes: 0
Labels: legacy-cxx
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

At all this problem exist only if the compiled MongoDB Driver is used in a DLL Target. If the same files will be used in a DLL, than this particular problem does not occur!

In my real target the problem occur when using the Macro GENOID or mongo::BSONObjBuilder().genOID()... Since I did not found the reason for this problem in my DLL project I was guessing this problem is because the Mongo driver is used in a DLL not an executable.

Additional hint:
For me it is mandatory to use the MongoDB driver in a DLL, the older drivers (v2.4.x and 2.5.x worked fine, the 26compat driver was only tested a little, therefore I can not tell anything about this.

How to reproduce the problem:
1. I used Visual Studio 2010SP1 to generate the MongoDB driver and I used Boost 1.51 to create the driver. I used the followed scons call:

scons.py --prefix=E:\LibraryBuild\mongo-cxx-driver-legacy-1.0.3\mongoDriver --cpppath=E:\LibraryBuild\boost_1_51_0 --libpath=E:\LibraryBuild\boost_1_51_0\stage\x64\lib-boost --64 install --dynamic-windows --sharedclient --dbg=on

I created also the remeining possible driver versions as stated in the description:

scons $ARGS install
scons $ARGS install --dbg=on
scons $ARGS install --dynamic-windows --sharedclient
scons $ARGS install --dynamic-windows --sharedclient --dbg=on

2. I created a DLL and I exported a function using the followed code:

#include <WinSock2.h>
#include <Windows.h>
#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver

extern "C" __declspec(dllexport) void Test()

{ mongo::OID oid; oid.init(); // <-- Here occurs the problem std::string str=oid.toString(); }

The exception occurs a oid.init(), in OID::init() the function setincrecment is called, in OID::Increment OID::Increment::next() the line is called, than in scoped_ptr.hpp in T* oparator->() the assert will be triggered because px is 0.



 Comments   
Comment by Andrew Morrow (Inactive) [ 09/Jul/15 ]

Happy to hear that adding the initialization fixed the problem. I've added some additional documentation to the wiki here:

https://github.com/mongodb/mongo-cxx-driver/wiki/Download-and-Compile-the-Legacy-Driver#using-the-driver-in-your-application

Comment by Thomas M. [X] [ 09/Jul/15 ]

Hello Andrew

You are correct, in my DLL Test code and in the real code also "mongo::client::initialize()" is missing. The real code is really old and uses currently the old (2.4.x) driver with MongoDB 2.4.13. When I did the code conversion I missed this important line. Maybe it should be marked red with an additional hint that this line is really important.
When I converted the code I focussed on compiler errors / warnings to be compatible to the new driver.

At all Andrew, a lot thanks, I guess I'd never found this out even if it would be easy to find. I was searching the problem for over a day in the code...

Best regards

Thomas

Comment by Andrew Morrow (Inactive) [ 09/Jul/15 ]

Thanks for the detailed writeup. First, I want to verify that you didn't literally type $ARGS for those builds, because if so that isn't going to do what you think it does. Given your command line example at the beginning, the correct set of invocations should be:

scons --prefix=E:\LibraryBuild\mongo-cxx-driver-legacy-1.0.3\mongoDriver --cpppath=E:\LibraryBuild\boost_1_51_0 --libpath=E:\LibraryBuild\boost_1_51_0\stage\x64\lib-boost --64 install
scons --prefix=E:\LibraryBuild\mongo-cxx-driver-legacy-1.0.3\mongoDriver --cpppath=E:\LibraryBuild\boost_1_51_0 --libpath=E:\LibraryBuild\boost_1_51_0\stage\x64\lib-boost --64 install --dbg=on
scons --prefix=E:\LibraryBuild\mongo-cxx-driver-legacy-1.0.3\mongoDriver --cpppath=E:\LibraryBuild\boost_1_51_0 --libpath=E:\LibraryBuild\boost_1_51_0\stage\x64\lib-boost --64 install --dynamic-windows --sharedclient
scons --prefix=E:\LibraryBuild\mongo-cxx-driver-legacy-1.0.3\mongoDriver --cpppath=E:\LibraryBuild\boost_1_51_0 --libpath=E:\LibraryBuild\boost_1_51_0\stage\x64\lib-boost --64 install --dynamic-windows --sharedclient --dbg=on

Next, I notice that you do not call ::mongo::client::Initialize in your test. With the legacy driver you must do so before using any part of the API. Please see the following documentation for details:

https://github.com/mongodb/mongo-cxx-driver/wiki/Configuring%20the%20Legacy%20Driver#the-mongoclientinitialize-function

Could you try adding a call to mongo::client::Initialize and see if the crash you are experiencing goes away?

Generated at Wed Feb 07 21:59:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.