[CXX-561] MONGO_INITIALIZER_GENERAL in src/mongo/bson/oid.cpp won't execute Created: 18/Mar/15  Updated: 06/Dec/16  Resolved: 18/Mar/15

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

Type: Bug Priority: Critical - P2
Reporter: Peng Xie Assignee: Adam Midvidy
Resolution: Done Votes: 0
Labels: crash, legacy-cxx
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File test.cc    

 Description   

MONGO_INITIALIZER_GENERAL in src/mongo/bson/oid.cpp fails to execute and makes

src/mongo/bson/oid.cpp

boost::scoped_ptr<AtomicUInt32> counter;

a null pointer.

This leads failure to call:

src/mongo/bson/oid.cpp

OID::Increment OID::Increment::next()

Running the binary compiled from

test.cc

//  scons --variant-dir=build   --disable-warnings-as-errors  --c++11=on --opt=off   --sharedclient  --ssl
//  g++ test.cc -lmongoclient -std=c++0x && ./a.out
 
#include <mongo/bson/bson.h>
 
int main() {
  mongo::OID::gen();
}

ends to:

a.out: /usr/include/boost/smart_ptr/scoped_ptr.hpp:99: T* boost::scoped_ptr<T>::operator->() const [with T = mongo::AtomicWord<unsigned int>]: Assertion `px != 0' failed.
Aborted



 Comments   
Comment by Peng Xie [ 18/Mar/15 ]

That works!

https://github.com/mongodb/mongo-cxx-driver/wiki/Configuring-the-Legacy-Driver also depicts it. I should have read it more carefully.

Thanks!

Comment by Adam Midvidy [ 18/Mar/15 ]

Hi Yuchen,

Sorry to hear you are having trouble with the legacy driver!

You must initialize the driver to ensure that Mongo initializer blocks are executed. You can do this by calling mongo::client::initialize or stack allocating a mongo::client::GlobalInstance in the main() block of your program.

The correct code is:

#include <mongo/bson/bson.h>
#include <mongo/client/init.h>
 
int main() {
  mongo::client::GlobalInstance instance;
  mongo::OID::gen();
}

Best,
Adam

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