Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C++ Segmentation Fault

The Problem

I am trying to connect to MongoDB in C++. The following code does actually compile. However, when I try to run the program, I get a segmentation fault.

- Edit -

This is what I get after running it in gdb (no change in source code or makefile):

GDB Run:

Starting program: /home/nathanw/devel/Linux/mkfarina-cpp/mkfarina 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff69ae700 (LWP 13314)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff69ae700 (LWP 13314)]
0x00007ffff6d79034 in pthread_mutex_unlock () from /lib/x86_64-linux-gnu/libpthread.so.0

GDB Where:

#0  0x00007ffff6d79034 in pthread_mutex_unlock () from /lib/x86_64-linux-gnu/libpthread.so.0
#1  0x00007ffff7bca948 in boost::detail::thread_data_base::~thread_data_base() () from /usr/local/lib/libboost_thread.so.1.52.0
#2  0x000000000046c74b in boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf1<void, mongo::BackgroundJob, boost::shared_ptr<mongo::BackgroundJob::JobStatus> >, boost::_bi::list2<boost::_bi::value<mongo::BackgroundJob*>, boost::_bi::value<boost::shared_ptr<mongo::BackgroundJob::JobStatus> > > > >::~thread_data() ()
#3  0x00007ffff7bc7d39 in thread_proxy () from /usr/local/lib/libboost_thread.so.1.52.0
#4  0x00007ffff6d75e9a in start_thread () from /lib/x86_64-linux-gnu/libpthread.so.0
#5  0x00007ffff6aa2cbd in clone () from /lib/x86_64-linux-gnu/libc.so.6
#6  0x0000000000000000 in ?? ()

Environment

- Ubuntu 12.04 LTS
- MongoDB 2.2.2
- MongoDB C++ Driver 2.2.2
- boost 1.52.0

The Source Code

#include <cstdlib>
#include <iostream>

// #include <cppcms/application.h>
// #include <cppcms/applications_pool.h>
// #include <cppcms/service.h>
// #include <cppcms/http_response.h>

#include "mongo/client/dbclient.h"

int main( int argc, char** argv ){
    try {
        mongo::DBClientConnection c;
        c.connect( "localhost" );

        std::cout << "connected ok" << std::endl;

    } catch( const mongo::DBException& e ){
        std::cout << "caught " << e.what() << std::endl;

    }

    return 0;

}

The Makefile

CXX = clang++

TARGET = mkfarina

FLAGS = -c -v -00

LIBRARIES = \
    -lbooster \
    -pthread \
    -lmongoclient \
    -lcppcms \
    -lboost_thread \
    -lboost_filesystem \
    -lboost_program_options \
    -lboost_system \


INCLUDE_PATHS = \
    -I/usr/local/include \
    -I/usr/local/include/boost \
    -I/usr/local/include/mongo \
    -I/usr/local/include/cppcms \
    -I/home/nathanw/devel/_include \


LIBRARY_PATHS = \
    -L/usr/lib \
    -L/usr/local/lib \


SOURCES = \
    main.cpp \


OBJECTS = $(SOURCES:.cpp=.o)

$(TARGET): $(OBJECTS)
    $(CXX) $(OBJECTS) $(LIBRARY_PATHS) $(LIBRARIES) -o $(TARGET)

%.o: %.cpp
    $(CXX) $(FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(LIBRARIES) $< -o $@

clean:
    rm -f $(TARGET) $(OBJECTS)
like image 884
Nathan Wehr Avatar asked Jan 31 '13 21:01

Nathan Wehr


2 Answers

The code above works fine...

It is most likely you are using different version of boost in code and in the compiled mongodb library.

Make sure:

  • Mongo DB uses exactly the same version of boost library and headers that you are using.
  • Make sure that they are looking on exactly the same headers.
  • Make sure libmongoclient is compiled with clang compiler and the boost library is compiled with clang one as well
  • Make sure you link with correct boost versions.
  • Make sure you use the same C++ flags for all compilations (i.e. stuff like C++0x)

It is very common case that you mix up different versions of boost also Boost is very sensitive to changes in flags and code/compiler.

For example Ubuntu 12.04 comes with Boost-1.46, so it is likely that you may include the OS specific files and not your version files, same for linking etc. Also you may accidentally use or link with OS mongo-db that was compiler against boost-1.46

like image 163
Artyom Avatar answered Oct 10 '22 18:10

Artyom


This error can happen if you are on OS X 10.9 Mavericks and mongo is using a different implementation of the C++ standard library than boost (libstdc++ vs libc++). To prevent this, when you build the C++ driver with scons be sure to pass:

--osx-version-min=10.9

An example build command might be:

scons --prefix=$HOME/mongo-client-install --extrapath=/usr/local/Cellar/boost/1.55.0_2 install-mongoclient --osx-version-min=10.9

like image 40
rkaplan Avatar answered Oct 10 '22 17:10

rkaplan