使用mongo c驱动程序

时间:2015-08-10 12:24:51

标签: mongo-c-driver mongo-cxx-driver

我有内存损坏,我不知道为什么。错误消息是:

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

我正在使用mongoc库版本1.1.10。这是我的代码片段:

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();

} 

我在bucle里面有这个函数来迭代插入。奇怪的是第一次插入是好的,但是当第二次插入时,会发生内存损坏。

基本上(伪代码):

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

如果它有任何帮助,我之前使用相同结果连接MySQL数据库。我安装了valgrind来检查是不是分配内存错误还是因为我没有释放内存。 valgrind没有报告内存错误。

我设法修复了在每次插入之前插入此行的错误:

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

在插入结束时:

mysql_library_end(); 

我认为错误可能类似,但我没有找到任何初始化和关闭mongodb库的函数。

要编译的命令行是:

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 个答案:

答案 0 :(得分:0)

据我所知,你的代码不应该使用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);

查看c-driver中包含的测试以获得大量示例。