Mongocxx segmentation fault issue

Hello everyone,

I work on a Linux application based on C++ & Qt and I try to properly build mongocxx and link to it. Everything works smoothly, but the app crashes on launch. Here is the stack trace:

Program received signal SIGSEGV, Segmentation fault.
0x0000555555d6d070 in bson_free ()
(gdb) where
#0  0x0000555555d6d070 in bson_free ()
#1  0x00007ffff3f21b63 in bson_string_free (string=0x555555e4d320, free_segment=free_segment@entry=false)
    at /home/lukasz/qliqsoft/mongo-c-driver/src/libbson/src/bson/bson-string.c:101
#2  0x00007ffff3f9086b in _set_platform_string (handshake=0x7ffff3ff7780 <gMongocHandshake>)
    at /home/lukasz/qliqsoft/mongo-c-driver/src/libmongoc/src/mongoc/mongoc-handshake.c:378
#3  _mongoc_handshake_init () at /home/lukasz/qliqsoft/mongo-c-driver/src/libmongoc/src/mongoc/mongoc-handshake.c:442
#4  0x00007ffff3f8ae33 in _mongoc_do_init () at /home/lukasz/qliqsoft/mongo-c-driver/src/libmongoc/src/mongoc/mongoc-init.c:140
#5  0x00007ffff456547f in __pthread_once_slow (once_control=0x7ffff3ff7734 <once>, init_routine=0x7ffff3f8adf0 <_mongoc_do_init>)
    at pthread_once.c:116
#6  0x00007ffff6c6cb7b in std::unique_ptr<mongocxx::v_noabi::instance::impl, std::default_delete<mongocxx::v_noabi::instance::impl> > core::v1::make_unique<mongocxx::v_noabi::instance::impl, void, std::unique_ptr<mongocxx::v_noabi::logger, std::default_delete<mongocxx::v_noabi::logger> > >(std::unique_ptr<mongocxx::v_noabi::logger, std::default_delete<mongocxx::v_noabi::logger> >&&) () from /lib/x86_64-linux-gnu/
#7  0x00007ffff6c6c8fd in mongocxx::v_noabi::instance::instance(std::unique_ptr<mongocxx::v_noabi::logger, std::default_delete<mongocxx::v_noabi::logger> >) () from /lib/x86_64-linux-gnu/
#8  0x00007ffff6c6c9d9 in mongocxx::v_noabi::instance::instance() () from /lib/x86_64-linux-gnu/
#9  0x000055555578b47f in main (argc=1, argv=0x7fffffffde88) at ../qliqdesktop/src/qliqstor/service/main.cpp:37
(gdb) quit
A debugging session is active.

So creating an mongocxx::instance variable is the very first thing that application does, but I get segmentation fault. I think that it is something related to mongocxx dependencies, so how I actually build the library?

I needed mongoc, so I cloned the official repository, built it, and installed using official guideline. I used version from r1.17 branch. I could install from Ubuntu repository, but that version is 1.16 and the newest mongocxx requires at least 1.17.

So that worked and I cloned mongocxx repository from releases/stable branch, built it and installed. Everything using commands from official guideline.

App builds without problems, ldd sees mongocxx and bsoncxx, but I get that segmentation fault. Do you have any ideas how to solve this issue?

Hi @Lukasz_Kosinski,

Thank you for including the stack trace. From that, I suspect the mongocxx::instance is going out of scope and being destroyed at some point during the lifetime of your application.

The mongocxx::instance should be created exactly once for the lifetime of the application (Tutorial for mongocxx)

If that is not the issue, can you include the relevant snippet from your application code where the mongocxx::instance is being created?

Thanks @Kevin_Albertson. I think that it is not the case as I’m pretty sure that I create only one instance.

The effect is the same if I run the following code:

#include <mongocxx/instance.hpp>

int main(int argc, char *argv[])
    mongocxx::instance mongoInstace;

    return 1;

Just wanted to ensure me :slight_smile:

@Lukasz_Kosinski the example code you provided to does not result in a SIGSEV on my system. Can you provide the CMake commands you used for configuring/installing the C and C++ drivers? The CMake configuration commands along with their output would be most helpful, along with the output of ldd ./a.out from the compiled example?

Thanks, @Roberto_Sanchez. I had a fresh look today and it looks like I had another, old driver in my include path. After removing that older driver it worked like charm.
Thanks for your proposals.

The thread can be closed.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.