1

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

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

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

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)
4

1 回答 1

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 中包含的测试以获取大量示例。

于 2015-08-24T13:06:05.947 回答