Memory corruption with mongo c driver

299 Views Asked by At

I have a memory corruption and I have no idea why. The error message is:

ERROR:
*** Error in `./server': malloc(): memory corruption (fast): 0x0000000000d743f0 *** 

I'm using the mongoc library version 1.1.10. This is a snippet of my code:

int gpsElements::guardaBasedeDatosMongodb(long imeiGps)
{
//mongoDB declarations
   mongoc_client_t *clientMongo;
   mongoc_collection_t *collection;

   const char *uristr="mongodb://192.168.0.16/";
   const char *collection_name = "13226008593168";
   //bson_t query;
   char *str;
    bson_error_t error;
    bson_oid_t oid;
    bson_t *doc;
    //bson *doc;

/* Initializes mongodb library */

mongoc_init ();

/* new client */
clientMongo= mongoc_client_new (uristr);


   if (!clientMongo) {
      fprintf (stderr, "Failed to parse URI.\n");
      return 0;
   }
}
cout<<"Conection ok"<<endl;


printf("before collection ");
collection = mongoc_client_get_collection (clientMongo, "gpscars", collection_name);
printf("All ok at this point");
    stringstream streamSat;
    string stringSat;
    streamSat << satelites;
    stringSat = streamSat.str();

    stringstream streamImei;
    string stringImei;
    streamImei << imeiGps;
    stringImei = streamImei.str();

    stringstream streamTimestamp;
    string stringTimestamp;
    streamTimestamp << timestamp;
    stringTimestamp = streamTimestamp.str();

    stringstream streamLongitude;
    string stringLongitude;
    streamLongitude << longitude;
    stringLongitude = streamLongitude.str();

    stringstream streamLatitude;
    string stringLatitude;
    streamLatitude << latitude;
    stringLatitude = streamLatitude.str();

    stringstream streamAltitude;
    string stringAltitude;
    streamAltitude << altitude;
    stringAltitude = streamAltitude.str();

    stringstream streamAngulo;
    string stringAngulo;
    streamAngulo << angle;
    stringAngulo = streamAngulo.str();

    stringstream streamSpeed;
    string stringSpeed;
    streamSpeed << speed;
    stringSpeed = streamSpeed.str();

    stringstream streamHdop;
    string stringHdop;
    streamHdop << hdop;
    stringHdop = streamHdop.str();
printf("All ok at this point too");

doc=(bson_t*)malloc(sizeof(bson_t));


bson_init(doc);

bson_append_utf8(doc,"imei",-1,stringImei.c_str(),-1);
bson_append_utf8(doc,"hora_gps",-1,stringTimestamp.c_str(),-1);
bson_append_utf8(doc,"longitud",-1,stringLongitude.c_str(),-1);
bson_append_utf8(doc,"latitud",-1,stringLatitude.c_str(),-1);
bson_append_utf8(doc,"altitud",-1,stringAltitude.c_str(),-1);
bson_append_utf8(doc,"angulo",-1,stringAngulo.c_str(),-1);
bson_append_utf8(doc,"satelites",-1,stringSat.c_str(),-1);
bson_append_utf8(doc,"velocidad",-1,stringSpeed.c_str(),-1);
bson_append_utf8(doc,"hdop",-1,stringHdop.c_str(),-1);
    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        printf ("%s\n", error.message);
    }

    bson_destroy(doc);

    free(doc);
    mongoc_collection_destroy (collection);
    mongoc_client_destroy (clientMongo);
    mongoc_cleanup();

} 

I have this function inside a bucle that iterate the insertion. The weird thing is the first insertion is ok, but when it does the second insertion the memory corruption happens.

Basically (pseudocode):

for(int i = 0; i<20; i++)
{
   database_insertion();
}

If it is of any help, previously I connected with a MySQL database with the same result. I installed valgrind to check that not was an allocate memory error or because I didn't free memory. No memory errors reported by valgrind.

I managed to fix the error inserting this line before every insertion:

if(mysql_library_init(0,NULL,NULL)==0){
   cout<<"MySQL library initialized"<<endl;
} else {
  cout<<"Unable to initialize MySQL"<<endl;
  exit(-1);
} 

And at the end of the insertion:

mysql_library_end(); 

I think the error could be similar, but I didn't found any function to initialize and close the mongodb library.

The command line to compile is:

g++ gpsData.cc gpsElements.cc IOElement.cc Pro3Data.cc server.cc crc.cc configData.cc configDataC.cc -o server -L/usr/lib/x86_64-linux-gnu -lboost_system -pthread -lboost_filesystem -lboost_program_options -lpthread -lboost_thread $(pkg-config --cflags --libs libmongoc-1.0)
1

There are 1 best solutions below

0
On

As far as I understand your code should not use malloc.

bson_t doc; 
bson_init(&doc);

bson_append_utf8(&doc,"imei",-1,"abc",-1);
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, &error)) {
  printf ("%s\n", error.message);
}

bson_destroy(&doc);

have a look at the tests contained in the c-driver to get lot of examples.