[CXX-562] Intial object name mongo::client::initialize(); represent Program received signal SIGSEGV, Segmentation fault Created: 18/Mar/15  Updated: 01/Apr/15  Resolved: 01/Apr/15

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

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


 Description   

Object mongo::client::intiailze() cannot initial mongo-client call to server when use legacy driver. After use gtest run mongo_connector binary file show as below.

(gdb) bt
#0 0x00007ffff7ffe2c8 in ?? ()
#1 0x0000000000412843 in std::_Function_base::~_Function_base() () at /usr/include/c++/4.8/functional:2030
#2 0x0000000000412f40 in std::function<std::auto_ptr<mongo::logger::Appender<mongo::logger::MessageEventEphemeral> > ()>::~function() () at /usr/include/c++/4.8/functional:2170
#3 0x0000000000412f5e in mongo::client::Options::~Options() () at /home/chatsiri/sda1/workspacecpp/mongo-cxx-driver/build/include/mongo/client/options.h:32
#4 0x00000000004132fb in connector::mongo_connector<scan_threat::InfectedFileInfo>::mongo_connector() () at mongo_connector.cpp:33

Legacy source is version:

commit 624146feb3bb74fe139b167af02d666e83f24e7d
Date: Sun Mar 15 18:04:25 2015 +0800

Command compiles legacy branch of C++ driver:

scons --sharedclient --extrapath=/home/chatsiri/sda1/workspacecpp/boost_1_57_0 --cpppath=/home/chatsiri/sda1/workspacecpp/boost_1_57_0 --libpath=/home/chatsiri/sda1/workspacecpp/boost_1_57_0/stage/lib --cxx=/usr/bin/g++-4.8 --cc=/usr/bin/gcc-4.8 --dbg=on --prefix=/home/chatsiri/sda1/workspacecpp/mongo-cxx-driver/build install

An example source code before was compiled to binary file.

class mongo_conector{
public:
mongo_conector()

{ mongo::client::initialize(); // Cannot intial show SIGSEGV,Segmentation fault }

...
};



 Comments   
Comment by rchatsiri [ 01/Apr/15 ]

Hi @Adam Midvidy,

Thank you for advise. In my programs, Other class call mongo_connector() constructor. It's still alive in memory for my case. I understand you point programs load only one constructor class. Would you close ticket-problem?

Thank in advances,
R.Chatsiri

Comment by Adam Midvidy [ 31/Mar/15 ]

Hi rchatsiri,

the issue is that you are stack allocating a mongo::client::GlobalInstance in the mongo_connector() constructor.
When the constructor finishes, the GlobalInstance is destructed, and the driver's internal state is destroyed.

You should instead create a GlobalInstance in the main() block of your program. A GlobalInstance object should only be created once, and should stay in scope for the duration of your program.

Comment by rchatsiri [ 26/Mar/15 ]

Hi Adam Midvidy,

Project complied by G+-4.8 which flag of standard library switch to -std=c+11. I found solving problem by initial connection object when implement global instance as below. It didn't show error after run test suite.

boost::scoped_ptr<mongo::DBClientBase> conn(cs.connect(err_msg));

That full description in my code write in github respository. Global Instance and connection object implement both in mongo_connector class.

mongo_connector.cpp

 namespace connector
{
template<typename MessageType, typename DBConnector>
mongo_connector<MessageType, DBConnector>::mongo_connector() : flag_connect(false)
{
              mongo::client::GlobalInstance instance;
              if(instance.initialized()) {
                   LOG(INFO)<<"failed to initialize the client driver: " << instance.status();
              }
 
}
template<typename MessageType, typename DBConnector>
bool mongo_connector<MessageType, DBConnector>::connector(const char *url, const char *port)
{
   std::string err_msg;
   std::string local(url);
   std::string port_(port);
   std::string url_conn(local.append(std::string(":")).append(port_));
   cs = mongo::ConnectionString::parse(url_conn, err_msg);
   if (!cs.isValid()) {
               LOG(INFO)<< "Error parsing connection string " << url_conn << ": " << err_msg;
     return flag_connect;
   }
         flag_connect = true;
         LOG(INFO)<<"Start Connection : " << url_conn;
     return flag_connect;
}
template<typename MessageType, typename DBConnector>
bool mongo_connector<MessageType, DBConnector>::insert(MessageType& msg)
{
     std::string err_msg;
     boost::scoped_ptr<mongo::DBClientBase> conn(cs.connect(err_msg));
     mongo::BSONObj tracethreat_bson = BSON(
    "uuid"<<msg.uuid()<<
    "machine_name"<<msg.machine_name()<<
    "ip"<<msg.ip()<<
    "encode_sig_type"<<msg.encode_sig_type()<<
    "file_name"<<msg.file_name()<<
    "status_result"<<msg.status_result()<<
    "virus_name"<<msg.virus_name()<<
    "binary"<<msg.binary()
);
conn->insert("tracethreat.infected_file_info", tracethreat_bson);
}
 
 
..
}// namespace connector

ref: https://github.com/treadstoneproject/tracethreat_client/blob/core_feature/src/connector/mongo_connector.cpp

Thank in advance,
R.Chatsiri

Comment by Adam Midvidy [ 25/Mar/15 ]

Hi rchatsiri,

What version of C++ are you compiling the driver with, and what version are you compiling your application with?

From the stack trace you gave it seems like the issue is a mismatched C++ version - either compiling the driver in C++11 mode and your application in C++03/98 mode or visa versa.

Comment by rchatsiri [ 18/Mar/15 ]

Hello Adam Midvidy,

In my project use google test of C++. Instance name mongo::client::GlobalInstance instance; cannot initial object when it's declares main object in the TEST(...) such

mongo_connector.cpp

 
class mongo_connector{
 
public:
 
   mongo_connector(){
      //call connector 
   }
 
};
 
TEST(mongo_connector_test, connecion_init){
    connector::mongo_connector  mongo_connector;
}
 
int main(int argc, char **argv)
{
    mongo::client::GlobalInstance instance;
   ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

It's present an error as

0 0x00000000004201c9 in __libc_csu_init ()
#1 0x00007fffffffc900 in ?? ()
#2 0x000000000041ffae in mongo::client::Options::~Options() () at /home/chatsiri/sda1/workspacecpp/mongo-cxx-driver/build/include/mongo/client/options.h:32
#3 0x0000000000000007 in ?? ()
#4 0x00000000004201cd in __libc_csu_init ()
#5 0x00007ffff574776d in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#6 0x00000000004120a9 in _start ()

My view, It's example class to call to instance object. Object name GlobalInstance should to call pass TDD class. Why cannot initials object in main class that is gtest?

Comment by Adam Midvidy [ 18/Mar/15 ]

Hey Rchatsiri,

It looks like you are calling mongo::client::initialize() in the constructor of your mongo_connector class. initialize() should only be called once in your program, and it must be called before any other driver functionality is used.

Can you try taking the mongo::client::initialize() out of the mongo_connector constructor and putting it in the main() function of your program before any other driver functions are called? There is also a convenience wrapper, mongo::client::GlobalInstance that will manage the lifecycle of the driver for you.

Your code would look like this:

#include <mongo/client/init.h>
 
int main(int argc, char** argc) {
    mongo::client::GlobalInstance instance;
    .... do stuff with mongo ..
}

Comment by rchatsiri [ 18/Mar/15 ]

Example on github wiki can run completed.

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

void run()

{ mongo::DBClientConnection c; c.connect("localhost"); }

int main() {
mongo::client::initialize();
try

{ run(); std::cout << "connected ok" << std::endl; }

catch( const mongo::DBException &e )

{ std::cout << "caught " << e.what() << std::endl; }

return EXIT_SUCCESS;
}

Command line to compile as below.

g++-4.8 tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_system -lboost_regex -o tutorial -I/home/chatsiri/sda1/workspacecpp/mongo-cxx-driver/build/include -L/home/chatsiri/sda1/workspacecpp/mongo-cxx-driver/build/
lib -L/home/chatsiri/sda1/workspacecpp/boost_1_57_0/stage/lib -I/home/chatsiri/sda1/workspacecpp/boost_1_57_0/ -Wl,-rpath,/home/chatsiri/sda1/workspacecpp/boost_1_57_0/stage/lib

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