Uploaded image for project: ' AGL Development'
  1. AGL Development
  2. SPEC-3584

memory leak introduced by json_object_new_object() solo

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Inactive
    • Icon: Minor Minor
    • None
    • None
    • None
    • None

      for example, 

      json_object *jresp = json_object_new_object();
      
      if(condition)
          return;

      here, we invoke json_object_new_object(),  and then don't  call any other json-c functions(such as  below:

      json_object_object_add(jresp, "adapter", 
      

      to transfer the owerships, then we should use json_object_put(jresp) to release the allocated memory.

       

      To verify my opinion, I make some test as below:

       

      $ cat json-test.c
      /* json-test.c */ 
      #include <stdio.h> 
      #include <stdio.h> 
      #include <json-c/json.h> 
      #include <mcheck.h> 
      int main() 
      { 
          struct json_object* obj; 
          mtrace(); 
          obj = json_object_new_object(); 
          return 0; 
      } 
      
      $ gcc -g -o json-test  json-test.c -I/usr/include/json-c -L/usr/lib -ljson-c
      $ export MALLOC_TRACE=mtrace.out
      $ ./json-test
      $ mtrace json-test mtrace.out
      mtrace json mtrace.out Memory not freed:
      -----------------
                 Address     Size     Caller
      0x000056264c4fe6a0     0x48  at 0x7f0e0ca33520
      0x000056264c4fe6f0     0x58  at 0x7f0e0ca36f83
      0x000056264c4fe750    0x200  at 0x7f0e0ca36fa5
      
      
      
      $ cat json-test-new.c
      #include <stdio.h>
      #include <stdio.h>
      #include <json-c/json.h>
      #include <mcheck.h>
      int main()
      { 
          struct json_object* obj; 
          mtrace(); 
          obj = json_object_new_object();        
          json_object_put(obj); 
          return 0;
      }
      
      $ gcc -g -o json-test-new json-test-new.c -I/usr/include/json-c -L/usr/lib -ljson-c 
      $ export MALLOC_TRACE=mtrace.out 
      $ ./json-test-new
      $ mtrace json-test-new mtrace.out
      No memory leaks.

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            LiXiaoming Li Xiaoming
            LiXiaoming Li Xiaoming
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: