cJSON icon indicating copy to clipboard operation
cJSON copied to clipboard

Validating for memory leak issue

Open gskrish7 opened this issue 2 years ago • 4 comments

I am creating a JSON like this: { "msg":"type-a", "uid":"op01kd", "key":"9kasHmSmsQhhBXTk", "node-ctx":{ "id":"r111", "type":"safety", "auth-code":"269hdHE4NiruSZS4JnnckA==" }, "global-code":"9cM2b/WkVMgsHSPknmJrDMZiOc8B+cFh5XbXVjMH5yw=" }

When I use JSON library to create a new JSON object and add objects one by one and finally(after consuming JSON) free the object using cJSON_Delete()/ This system obviously occupies heap after some time and application exits as malloc fails. If I replace JSON operation with simple snprintf to construct JSON string manually, it is working fine. Is there any memory leak on version 1.7.10? Could we validate the library for memory leak check with tool like valgrind?

gskrish7 avatar Mar 04 '22 08:03 gskrish7

Show your code. I suspect you may not be freeing the cJSON resources properly.

mbratch avatar Mar 04 '22 11:03 mbratch

Sure, after adding elements; do I need to free node objects individually or does cJSON_Delete(root) will delete everything?

gskrish7 avatar Mar 04 '22 12:03 gskrish7

If you are adding elements under root and you didn't "detach" any of them, then freeing root should free everything in the element tree.

To do a more complete assessment, source code snippet would be needed.

On Mar 4, 2022, 7:06 AM, at 7:06 AM, gskrish7 @.***> wrote:

Sure, after adding elements; do I need to free node object individually or does cJSON_Delete(root) will delete everything?

-- Reply to this email directly or view it on GitHub: https://github.com/DaveGamble/cJSON/issues/670#issuecomment-1059105517 You are receiving this because you commented.

Message ID: @.***>

mbratch avatar Mar 04 '22 14:03 mbratch

@gskrish7 I tried something locally, and I suspect like @mbratch, that you may be failing to free some external resource.

Are you printing the object? If you call cJSON_Print(root), malloc is used. Be sure to free the string that cJSON_Print returns.

static void create_objects(void)
{
    cJSON *root = NULL;
    cJSON *fmt = NULL;

    root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "msg", "type-a");
    cJSON_AddStringToObject(root, "uid", "op01kd");
    cJSON_AddStringToObject(root, "key", "9kasHmSmsQhhBXTk");

    cJSON_AddItemToObject(root, "node-ctx", fmt = cJSON_CreateObject());

    cJSON_AddStringToObject(fmt, "id", "r111");
    cJSON_AddStringToObject(fmt, "type", "safety");
    cJSON_AddStringToObject(fmt, "auth-code", "269hdHE4NiruSZS4JnnckA==");
    cJSON_AddStringToObject(root, "global-code", "9cM2b/WkVMgsHSPknmJrDMZiOc8B+cFh5XbXVjMH5yw=");
    
    cJSON_Delete(root);
}

==2806== HEAP SUMMARY:
==2806==     in use at exit: 0 bytes in 0 blocks
==2806==   total heap usage: 25 allocs, 25 frees, 4,836 bytes allocated
==2806==
==2806== All heap blocks were freed -- no leaks are possible

zepthro avatar Jul 10 '22 20:07 zepthro