c++/c static lib: reciving SIGSEGV in _mongoc_client_new_from_uri

123 views
Skip to first unread message

javantamt

unread,
Jun 15, 2017, 9:15:39 AM6/15/17
to mongodb-user
Hey there,
my code is working with the normal compiled dynamic libraries. (bit older in lib version)
Now I tried it to compile bsonc mongoc bsoncxx and mongocxx as static libs. (latest stable) bsoncxx/mongocxx[3.1.1] mongoc/bsonc [1.6.3].
Because I've no idea where to start i simply have collected as much informationes as possible. Code, valgrind-output, building steps of the static library.

Can I deliver some more infos, try some more things? Compile and run an example?
Is this a bug in my programm? -> would say no, smae code compiling dynamical fine.
Can I test the Argument of mongocxx::client() if it is valid, before calling the function?
May it have to do with a misstake in compiling the libs?

Greatings javentamt



Code:
  std::cout << "Build MongoDB-Uri: " << strMongoDbUri << std::endl;
 
// Output: Build MongoDB-Uri: mongodb://conversion-server:my-pa...@127.0.0.1:27017/MyDatabase
 
try{

 
auto blubb = mongocxx::uri(strMongoDbUri);    // blubb for figuring out in which function the error occured...
 
 
// in this line the error occured
  conn
= mongocxx::client(blubb);



Valgrind output:
==9537== Invalid read of size 8
==9537==    at 0x4A0E9E: _mongoc_client_new_from_uri (in /Static/MyBinary)
==9537==    by 0x463B05: mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&) (in /Static/MyBinary)
==9537==    by 0x44DF0C: CMongo::CMongo() (CMongo.cpp:61)
           
...callstack
==9537==    by 0x4152B4: CThread::run() (CThread.cpp:223)
==9537==  Address 0xe0 is not stack'd, malloc'd or (recently) free'd
==9537==
==9537==
==9537== Process terminating with default action of signal 11 (SIGSEGV)
==9537==  Access not within mapped region at address 0xE0
==9537==    at 0x4A0E9E: _mongoc_client_new_from_uri (in /Static/MyBinary)
==9537==    by 0x463B05: mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&) (in /Static/MyBinary)
==9537==    by 0x44DF0C: CMongo::CMongo() (CMongo.cpp:61)
            ...callstack
==9537==    by 0x4152B4: CThread::run() (CThread.cpp:223)
==9537==  If you believe this happened as a result of a stack
==9537==  overflow in your program'
s main thread (unlikely but
==9537==  possible), you can try to increase the size of the
==9537==  main thread stack using the --main-stacksize= flag.
==9537==  The main thread stack size used in this run was 8388608.

Compiling with:

Invoking: GCC C++ Compiler
g
++ -std=c++0x -DMONGOCXX_STATIC -DBSONCXX_STATIC -DMONGOC_STATIC -DBSON_STATIC -I"/StaticLibaries/include" -I"/StaticLibaries/include/bsoncxx/v_noabi" -I"/StaticLibaries/include/mongocxx/v_noabi" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/app/timer/CTimerComponent.d" -MT"src/app/timer/CTimerComponent.o" -o "src/app/timer/CTimerComponent.o" "../src/app/timer/CTimerComponent.cpp"

Linking with

Building target: MyBinary
Invoking: GCC C++ Linker
g
++ -L"/home/lambda/blinkit_CServ/StaticLibaries/lib" -o "MyBinary"  ./src/base/allMyOfiles.o   -lmongocxx -lbsoncxx -lmongoc-1.0 -lssl -lcrypto -lbson-1.0 -lPocoNet -lPocoFoundation -ldl -lrt -lcurl -lpthread


-------------------------------------------------------------------

I build the Libs with:

c libs with ./configure --disable-automatic-init-and-cleanup --enable-static --prefix=/my/own/folder/StaticLibaries --with-libbson=bundled
c
++ libs with -DBSONCXX_POLY_USE_MNMLSTC=1 (at least it tried to downloade something from gitlab)
cmake
-DLIBMONGOC_DIR:STRING="/my/own/folder/StaticLibaries" -DBUILD_SHARED_LIBS:BOOL="0" -DLIBBSON_DIR:STRING="/my/own/folder/StaticLibaries" -DCMAKE_INSTALL_PREFIX:PATH="/my/own/folder/StaticLibaries" ..



Build configuration:
  Enable debugging (slow)                          : no
  Enable extra alignment (required for 1.0 ABI)    : yes
  Compile with debug symbols (slow)                : no
  Enable GCC build optimization                    : yes
  Code coverage support                            : no
  Cross Compiling                                  : no
  Big endian                                       : no
  Link Time Optimization (experimental)            : no

Documentation:
  man                                              : no
  HTML                                             : no


libmongoc 1.6.3 was configured with the following options:

Build configuration:
  Enable debugging (slow)                          : no
  Compile with debug symbols (slow)                : no
  Enable GCC build optimization                    : yes
  Enable automatic init and cleanup                : no
  Enable maintainer flags                          : no
  Code coverage support                            : no
  Cross Compiling                                  : no
  Fast counters                                    : no
  Shared memory performance counters               : yes
  SASL                                             : no
  SSL                                              : openssl
  Libbson                                          : bundled

javantamt

unread,
Jun 15, 2017, 7:58:38 PM6/15/17
to mongodb-user
Now build dynamic on different machine, problem remaining
I tried a lot, now I was gonna build the whole thing dynamic. But it didn't work too...
Good thing: I can reproduce it. The old version of the lib I've build bsonc and mongoc in separate. this has worked for me.
Here the approach I've choosen is the --with-libbson=bundled
Basically the main thing I've changed. so maybe this might be a hint.

I really would be happy if somebody can tell me how to buiild this thing in a correct way. How ever I can support you in fixing this I'll do.



VERSION_MONGOC=1.6.3
VERSION_MONGOCXX
=3.1.1
UBUNTU
=16.04

cd $
{LOCAL_DIR}/mongo-c-driver-${VERSION_MONGOC}
./configure \
--disable-automatic-init-and-cleanup \
--prefix=${THE_LIB_DIR} \
--with-libbson=bundled

make
-j4
make install

cd $
{LOCAL_DIR}/mongo-cxx-driver-r${VERSION_MONGOCXX}

PKG_CONFIG_PATH
=/usr/local/lib/pkgconfig cmake \
 
-DCMAKE_BUILD_TYPE=Release \
 
-DCMAKE_INSTALL_PREFIX=${THE_LIB_DIR} \
 
-DLIBBSON_DIR=${THE_LIB_DIR} \
 
-DLIBMONGOC_DIR=${THE_LIB_DIR}

make
-j4
make install

javantamt

unread,
Jun 16, 2017, 6:20:20 AM6/16/17
to mongodb-user
Build bsonc separate.

same behaviour:

valgrind output:
==23948== Invalid read of size 8
==23948==    at 0x839D4B6: _mongoc_client_new_from_uri (in /usr/local/lib/libmongoc-1.0.so.0.0.0)
==23948==    by 0x5A0C527: mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&) (in /usr/local/lib/libmongocxx.so.3.1.1)
           
... Callstack
==23948==    by 0x4124D4: CThread::run() (CThread.cpp:223)
==23948==  Address 0x60 is not stack'd, malloc'd or (recently) free'd

./configure of mongo-c

Build configuration:
  Enable debugging (slow)                          : no
  Compile with debug symbols (slow)                : no
  Enable GCC build optimization                    : yes
  Enable automatic init and cleanup                : no
  Enable maintainer flags                          : no
  Code coverage support                            : no
  Cross Compiling                                  : no
  Fast counters                                    : no
  Shared memory performance counters               : yes
  SASL                                             : no
  SSL                                              : openssl
  Libbson                                          : system


Documentation:
  man                                              : no
  HTML                                             : no

On Thursday, 15 June 2017 15:15:39 UTC+2, javantamt wrote:

javantamt

unread,
Jun 16, 2017, 6:43:37 AM6/16/17
to mongodb-user
It's not a solution, but a workaround:
configurating mongoc without
--disable-automatic-init-and-cleanup

it is working. Not happy with that. Maybe somebody should have a look into it or tell me, what I did wron.
thx



On Thursday, 15 June 2017 15:15:39 UTC+2, javantamt wrote:

A. Jesse Jiryu Davis

unread,
Jun 16, 2017, 7:32:20 AM6/16/17
to mongodb-user
Hi, are you calling "mongoc_init()" anywhere in your code? If not, then that is the problem: you must call "mongoc_init()" before using any other C Driver functions.

javantamt

unread,
Jun 16, 2017, 11:05:28 AM6/16/17
to mongodb-user
Ok, but I'm only using the mongocxx driver.
I know that cxx is on top of c-driver, but this should hide it.
So why this one is not caring for the mongoc_init() in my case?

Hannes Magnusson

unread,
Jun 16, 2017, 12:27:17 PM6/16/17
to mongodb-user
The c++ way to call mongoc_init() is:
mongocxx::instance instance{};

This will initialize everything, and must be called exactly once and must be available through the lifetime of your mongocxx use.
See https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/tutorial/#make-a-connection

-Hannes

javantamt

unread,
Jun 16, 2017, 5:47:20 PM6/16/17
to mongodb-user
Hey there, thank you for your both replies!
I admit, I never used 'instance'. Might be the reason of this trouble. Will do better.


But there is a question pending. I'm developing this program, as long as this account is existing an I never had any problems with this thing. I used the class below in a function where I needed a dbconnection. Leaving the function after lets say 3 exchanges with the db and so destroyed the class. Same function again, again the constructor and destructor. ( honest ratings and ideas for improvement? pleas go ahead ; )

class MyMongoClass{
 
CMongo();
 mongocxx
::client conn;
 
class mongocxx::collection collTasks;  
}

MyMongoClass::MyMongoClass(){
  std
::string MyUriString= .........;

 
auto blubb =   mongocxx::uri(strMongoDbUri);

  conn
= mongocxx::client(blubb);
 
//conn = mongocxx::client(mongocxx::uri(strMongoDbUri));
  collJobs
= conn[MyConfiguration.databaseName][MyConfiguration.collJobs];
}


Why has this worked all the time? And why no longer. I'm curious. Going to understand it.
Can I use the same connection ( mongocxx::client ) during the whole program execution? Would this bring me more speed in execution?
What happens if the dbconnection is interrupted, would it reconnect?

I've read the tutorial. Wished I had it, when I was starting to work with the new cxx driver. You put lots of work in it! Cool!

Is there a link how the connection thing is working in multithreadded programs? ( Otherwise I would maybe open a new question to don't mix the content)

Thank you,
javantamt

Sam Rossi

unread,
Jun 16, 2017, 5:58:28 PM6/16/17
to mongodb-user
Hi Javantamt! mongocxx::client cannot be shared across multiple threads; you can read more about the driver's thread safety here and about how to use connection pools (the way to utilize multiple threads) here. Let me know if you have any questions about the content of either of those pages!

Hannes Magnusson

unread,
Jun 16, 2017, 5:59:26 PM6/16/17
to mongod...@googlegroups.com
See https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/thread-safety/
for thread safety documentations.

If you never instanciated the "mongocxx::instance" then all bets are
off if things will work or not as there are a lot of possible error
conditions.
You must instanciate mongocxx::instance exactly once, and have it
available through the lifetime of the program, not per thread.

a client is not thread safe, only the pool is. You should get a client
from the pool for each thread you use.


Failing these requirements results in the application "maybe works, usually".

-Hannes
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> https://docs.mongodb.com/manual/support/
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "mongodb-user" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/mongodb-user/Zfh7xzPt7Kw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/3620da0b-2125-4ad2-9930-4155aa9fe8c5%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

javantamt

unread,
Jun 16, 2017, 8:14:41 PM6/16/17
to mongodb-user
Thx for the links and the detailed answers,
they are helping a lot!

Right now I think I don't have any more questiones =).
Reply all
Reply to author
Forward
0 new messages