内存泄漏:扫描valgrind时可能的内存

时间:2019-06-20 06:49:59

标签: c valgrind cjson

通过Valgrind扫描代码时,我看到了一些随机行为。我已经释放了所有可能的内存块,但我仍然看到Valgrind说没有正确释放1个块。

目标:使用cJSON.c创建3级json,然后在第二个块中修改json格式。

    #include<stdio.h>
    #include<stdlib.h>
    #include"cJSON.h"


    int main () {

    cJSON *root = NULL, *root1 = NULL , *arrays = NULL, *array = NULL;

           root = cJSON_CreateObject();
           root1 = cJSON_CreateObject();
           arrays = cJSON_CreateArray();

           cJSON_AddItemToObject(root,"check",root1);
    cJSON_AddItemToObject(root1, "innercheck",cJSON_CreateString("just to check"));

           cJSON_AddItemToObject(root1, "array", arrays);
           cJSON_AddItemToArray(arrays, array = cJSON_CreateObject());
   cJSON_AddItemToObject(array, "innnerarray", cJSON_CreateNumber(1));

           char *out = NULL;
           printf("===================================================\n");
           out = cJSON_Print(root);
           printf("%s\n", out);

           /* further operation now going to extract the value from json and readd new key */

           cJSON *json_param = NULL, *exe1 = NULL , *exe2 = NULL;

           json_param = cJSON_Parse(out);

           exe1 = cJSON_GetObjectItem(json_param , "check");
           cJSON_DeleteItemFromObject(exe1, "check");

           exe2  = cJSON_CreateObject();
           cJSON_AddItemToObject(exe2, "test", exe1);


           char *out2 = NULL;
           out2 = cJSON_Print(exe2);
           printf("====================================================\n");
           printf("%s\n", out2);

           if(root) {
                   cJSON_Delete(root);
                   root = NULL;
           }

           if(json_param) {
                   cJSON_Delete(json_param);
                   json_param = NULL;
           }

           if(out) {
                   free(out);
                   out = NULL;
           }

           if(out2) {
                   free(out2);
                   out2 = NULL;
           }

           return 0;
    }

Valgrind扫描报告:

    ==23708== Memcheck, a memory error detector
    ==23708== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==23708== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
    ==23708== Command: ./object
    ==23708== 
    ===================================================
    {
        "check":    {
            "innercheck":   "just to check",
            "array":    [{
                    "innnerarray":  1
                }]
        }
    }
    ====================================================
    {
        "test": {
            "innercheck":   "just to check",
            "array":    [{
                    "innnerarray":  1
                }]
        }
    }
    ==23708== 
    ==23708== HEAP SUMMARY:
    ==23708== in use at exit: 64 bytes in 1 blocks
    ==23708== 

> total heap usage: 29 allocs, 28 frees, 2,661 bytes allocated

    ==23708== 
    ==23708== 64 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==23708==    at 0x4C2FDFB: malloc (vg_replace_malloc.c:309)
    ==23708==    by 0x108F47: cJSON_New_Item (cJSON.c:214)
    ==23708==    by 0x10C9F8: cJSON_CreateObject (cJSON.c:2410)
    ==23708==    

> by 0x108BFA: main (object.c:35)

    ==23708== 
    ==23708== LEAK SUMMARY:
    ==23708==    definitely lost: 64 bytes in 1 blocks
    ==23708==    indirectly lost: 0 bytes in 0 blocks
    ==23708==      possibly lost: 0 bytes in 0 blocks
    ==23708==    still reachable: 0 bytes in 0 blocks
    ==23708==         suppressed: 0 bytes in 0 blocks
    ==23708== 
    ==23708== For lists of detected and suppressed errors, rerun with: -s
    ==23708== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

我已经释放了所有可能的内存块,但无法弄清为什么我仍然得到1个块却没有正确释放。

1 个答案:

答案 0 :(得分:3)

似乎您的代码中有两个问题。

首先,您需要在退出之前释放exe2对象(使用cJSON_CreateObject()分配的对象)。

cJSON_Delete(exe2);

第二...您需要先将check对象与json_param分离,然后再将其添加到exe2对象中。否则,当您调用cJSON_Delete(exe2)时,您将获得双倍的空闲时间-库将尝试释放两次相同的对象(因为同一对象作为一个子对象已附加到多个对象上)。

所以改变

    exe1 = cJSON_GetObjectItem(json_param, "check");
    cJSON_DeleteItemFromObject(exe1, "check");

简单

    exe1 = cJSON_DetachItemFromObject(json_param, "check");

请注意,我删除了行cJSON_DeleteItemFromObject(exe1, "check");-据我所知,这是多余的(除非您打算删除innercheck元素)。

相关问题