cJSON_Delete();导致 free():无效指针:0xb5db1e18
cJSON_Delete(); causes free(): invalid pointer: 0xb5db1e18
代码
cJSON *body = cJSON_Duplicate(record, true);
cJSON_AddItemToObject(body, KEY_LOG, log);
cJSON *array_pl = cJSON_CreateArray();
cJSON_AddItemToArray(array_pl, body);
cJSON *payload = cJSON_CreateObject();
cJSON_AddItemToObject(payload, KEY_PAYLOAD, array_pl);
cJSON_Delete(body);
cJSON_Delete(array_pl);
cJSON_Delete(payload);
每当我使用 cJSON_Delete() 程序崩溃并给出错误 *** Error in `./sample': free(): invalid pointer: 0xb5db1e18 ***
我对此进行了很多搜索,但找不到解决方案。
请告诉我这是如何工作的?
引用自https://github.com/DaveGamble/cJSON,所以请阅读文档。
Important: If you have added an item to an array or an object already, you mustn't delete it with cJSON_Delete. Adding it to an array or object transfers its ownership so that when that array or object is deleted, it gets deleted as well.
参考:Working with the data structure
所以你只需要释放 body
它就会释放里面分配的项目。删除其他两个删除将解决问题。
cJSON_Delete(body);
代码
cJSON *body = cJSON_Duplicate(record, true);
cJSON_AddItemToObject(body, KEY_LOG, log);
cJSON *array_pl = cJSON_CreateArray();
cJSON_AddItemToArray(array_pl, body);
cJSON *payload = cJSON_CreateObject();
cJSON_AddItemToObject(payload, KEY_PAYLOAD, array_pl);
cJSON_Delete(body);
cJSON_Delete(array_pl);
cJSON_Delete(payload);
每当我使用 cJSON_Delete() 程序崩溃并给出错误 *** Error in `./sample': free(): invalid pointer: 0xb5db1e18 ***
我对此进行了很多搜索,但找不到解决方案。
请告诉我这是如何工作的?
引用自https://github.com/DaveGamble/cJSON,所以请阅读文档。
Important: If you have added an item to an array or an object already, you mustn't delete it with cJSON_Delete. Adding it to an array or object transfers its ownership so that when that array or object is deleted, it gets deleted as well.
参考:Working with the data structure
所以你只需要释放 body
它就会释放里面分配的项目。删除其他两个删除将解决问题。
cJSON_Delete(body);