json-parser/json-builder

Building simple doc leaks memory

cabeaulac opened this issue · 3 comments

The simple CUnit test function below leaks memory when run in Valgrind.
Maybe I'm doing something wrong. Please let me know if I'm doing something wrong or this is a real memory leak. Thanks.

int build_simple_doc_test() {
    json_value * a_doc = json_object_new(0);
    json_value * dh_body = json_object_new(0);
    json_value * host = json_string_new("192.168.1.15");

    json_object_push(dh_body, "host", host);
    json_object_push(a_doc, "DebugHost", dh_body);

    char * buf = malloc(json_measure(a_doc));
    json_serialize(buf, a_doc);

    printf("%s\n", buf);
    free(buf);
    json_builder_free(a_doc);
}

Results from running with

valgrind my_test_app

Suite: json_input_test
  Test: build DebughHost response ...{ "DebugHost": { "host": "192.168.1.15" } }
passed

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests      1      1      1      0        0
             asserts      0      0      0      0      n/a

Elapsed time =    0.040 seconds
==16284== 
==16284== HEAP SUMMARY:
==16284==     in use at exit: 5 bytes in 1 blocks
==16284==   total heap usage: 17 allocs, 16 frees, 771 bytes allocated
==16284== 
==16284== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==16284==    at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==16284==    by 0x4066FC: json_object_push_length (json-builder.c:219)
==16284==    by 0x4066B1: json_object_push (json-builder.c:208)
==16284==    by 0x403945: build_simple_doc_test (json_test.c:91)
==16284==    by 0x4C124E4: run_single_test (in /usr/local/lib/libcunit.so.1.0.1)
==16284==    by 0x4C1211E: run_single_suite (in /usr/local/lib/libcunit.so.1.0.1)
==16284==    by 0x4C0FA66: CU_run_all_tests (in /usr/local/lib/libcunit.so.1.0.1)
==16284==    by 0x4C14311: basic_run_all_tests (in /usr/local/lib/libcunit.so.1.0.1)
==16284==    by 0x4C140AF: CU_basic_run_tests (in /usr/local/lib/libcunit.so.1.0.1)
==16284==    by 0x404058: main (json_test.c:245)
==16284== 
==16284== LEAK SUMMARY:
==16284==    definitely lost: 5 bytes in 1 blocks
==16284==    indirectly lost: 0 bytes in 0 blocks
==16284==      possibly lost: 0 bytes in 0 blocks
==16284==    still reachable: 0 bytes in 0 blocks
==16284==         suppressed: 0 bytes in 0 blocks
==16284== 
==16284== For counts of detected and suppressed errors, rerun with: -v
==16284== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

Thanks, should be fixed in d7ebc7a.

Thanks. Will try fix tomorrow.

-Chad

Sent from my iPhone

On Oct 5, 2014, at 3:19 PM, James McLaughlin notifications@github.com wrote:

Thanks, should be fixed in d7ebc7a.


Reply to this email directly or view it on GitHub.

Awesome. Simple unit test passes now. Thanks for this fix. I'll continue to write more sophisticated CUnit tests and finish my evaluation of this parser now.

==11797== HEAP SUMMARY:
==11797==     in use at exit: 0 bytes in 0 blocks
==11797==   total heap usage: 17 allocs, 17 frees, 771 bytes allocated
==11797== 
==11797== All heap blocks were freed -- no leaks are possible