mongoCXX: SIG: SEGFAULT while calling find_one()

167 views
Skip to first unread message

javantamt

unread,
May 2, 2016, 5:54:58 PM5/2/16
to mongodb-user
Hey there!

I'm trying to implement some easy wrapper for mongoDB in withe the new cxx driver.
Calling in this assambly the funcion find_one() is throwing a segfault. I don't know why.
It looks like an obvious misstake...

Member function:

int CMongo::FindOne(std::string strJSONFind, std::string strJSONOrderBy, core::v1::optional<bsoncxx::v_noabi::document::value> &docReturn ){

 
int iErr = 0;

  mongocxx
::options::find MyFindOptions{};
 
MyFindOptions.sort(bsoncxx::from_json(strJSONOrderBy).view());
 
// core::v1::optional<bsoncxx::v_noabi::document::value> XXX;

 
try{
 
//docReturn = collJobs.find_one(bsoncxx::from_json(strJSONFind).view(),MyFindOptions);    // SEGFAULT
 
// XXX = collJobs.find_one(bsoncxx::from_json(strJSONFind).view(),MyFindOptions); // segfault
   
// auto i = collJobs.find_one(bsoncxx::from_json(strJSONFind).view(),MyFindOptions); // segfault
   
auto i = collJobs.find_one(bsoncxx::from_json(strJSONFind).view(),MyFindOptions);  // segfault

Class header:
class CMongo {
public:
 
CMongo();

 
int FindOne(
      std
::string strJSONFind,
      std
::string strJSONOrderBy,
      core
::v1::optional<bsoncxx::v_noabi::document::value> &docReturn );
 
virtual ~CMongo();

private:
  mongocxx
::client conn;    // connection to database
 
class mongocxx::v_noabi::collection collJobs;    
};



Class Constructor:
CMongo::CMongo() {
 
// TODO Put this in every init functions withe a question of existence
  mongocxx
::client conn(mongocxx::uri("Mongo credentiales" ));
  collJobs
= conn["Database"]["collection"];
}

Function Call:
  CMongo MyMongo;
  core
::v1::optional<bsoncxx::v_noabi::document::value> ReturnVal;
 
MyMongo.FindOne("{\"started\":false}","{\"date\":1,\"priority\":1}",ReturnVal);



Andrew Morrow

unread,
May 2, 2016, 6:13:25 PM5/2/16
to mongod...@googlegroups.com


On Mon, May 2, 2016 at 5:54 PM, javantamt <java...@gmail.com> wrote:
Hey there!



 
Calling in this assambly the funcion find_one() is throwing a segfault. I don't know why.
It looks like an obvious misstake...

I think I know what the problem is, see below.

 

Member function:

int CMongo::FindOne(std::string strJSONFind, std::string strJSONOrderBy, core::v1::optional<bsoncxx::v_noabi::document::value> &docReturn ){

 
int iErr = 0;

  mongocxx
::options::find MyFindOptions{};
 
MyFindOptions.sort(bsoncxx::from_json(strJSONOrderBy).view());
 
// core::v1::optional<bsoncxx::v_noabi::document::value> XXX;

 
try{
 
//docReturn = collJobs.find_one(bsoncxx::from_json(strJSONFind).view(),MyFindOptions);    // SEGFAULT
 
// XXX = collJobs.find_one(bsoncxx::from_json(strJSONFind).view(),MyFindOptions); // segfault
   
// auto i = collJobs.find_one(bsoncxx::from_json(strJSONFind).view(),MyFindOptions); // segfault
   
auto i = collJobs.find_one(bsoncxx::from_json(strJSONFind).view(),MyFindOptions);  // segfault


Note: this isn't your problem, but you shouldn't need the .view calls above. The bsoncxx::from_json is an rvalue and should be able to be passed directly to find_one.


 
Class header:
class CMongo {
public:
 
CMongo();

 
int FindOne(
      std
::string strJSONFind,
      std
::string strJSONOrderBy,
      core
::v1::optional<bsoncxx::v_noabi::document::value> &docReturn );
 
virtual ~CMongo();

private:
  mongocxx
::client conn;    // connection to database
 
class mongocxx::v_noabi::collection collJobs;    
};



Class Constructor:
CMongo::CMongo() {
 
// TODO Put this in every init functions withe a question of existence
  mongocxx
::client conn(mongocxx::uri("Mongo credentiales" ));
  collJobs
= conn["Database"]["collection"];
}


I think you haven't actually initialized the member CMongo::conn here - you have shadowed it, and created a temporary mongocxx::client object called 'conn' on the stack. You then use that to initialize CMongo::collJobs, and it immediately goes out of scope.

That is not a legal usage: the collection object must outlive the connection object that produced it.

Can you see if your segfault is resolved if you write this as:

conn = mongocxx:client(mongocxx::uri("Mongo credentiales" ));

Even better would be to initialize conn in the member initializer list in CMongo::CMongo.

I recommend running your application under valgrind or AddressSanitizer to find these sorts of mistakes.

javantamt

unread,
May 24, 2016, 11:16:49 AM5/24/16
to mongodb-user

Note: this isn't your problem, but you shouldn't need the .view calls above.

It was my problem. Thx for this =).

Reply all
Reply to author
Forward
0 new messages