0

cJSON memory leak is a post where a memory leak occured. But the problem n this case is the cJSON_Print() function.

I did not even use this function (have commented it for the time being) and have still a memory leakage. My ode looks like this

void myFunc(cJSON* ptr)
{
    /*some code */
     // I have used some sint32 numbers from another library for simplicity
     // i will use int
     int num = 30
     cJSON_AddItemToArray(pt_data,cJSON_CreateNumber(num));
}

int main()
{
    cJSON *root =cJSON_CreateObject();
    cJSON *pt_PPC= cJSON_CreateArray();
    cJSON_AddItemToObject(root,"PowerPC",pt_PPC);
    cJSON *pt_data = cJSON_CreateArray();
    cJSON_AddItemToArray(pt_PPC,pt_data);
    int i;
    for(i=0;i<10;i++)
       myFunc(pt_PPC);
    cJSON_Delete(root);
    return 0;
}

The memory increases with time. Any suggestions?

Sled
  • 18,541
  • 27
  • 119
  • 168
Modasser
  • 3
  • 4
  • Have you tried calling `myFunc` prior to adding `pt_PPC` to the `root` object? – Kevin Richardson Sep 10 '15 at 15:38
  • Yes just tried it but does not change anything. I dont know if it would make any difference. Maybe the `cJSON_Delete()` function has problem deleting the objects created in another scope. – Modasser Sep 10 '15 at 16:06
  • I was hoping it wouldn't make a difference, but at the time, I thought you had posted the entirety of your code and didn't see another problem (other than the syntax errors). I've been using cJSON for awhile and haven't noticed any bugs that lead to leaks. – Kevin Richardson Sep 10 '15 at 16:09
  • I am running this on an embedded system. So memory is very cruicial. My application gets killed because of this. I have narrowed down the problem to the JSON problem. If i do not use this function memory stays stable. – Modasser Sep 10 '15 at 16:12
  • Your edited code still does not compile. – trenki Sep 10 '15 at 16:28

3 Answers3

1

I've tried your code in VS2015 and found that your myFunc function does not even compile! The function cJSON_AddItemToObject takes three parameters and num is not even defined.

I tried with the following code:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include "cJSON.h"

void myFunc(cJSON* ptr)
{
    /*some code */
    cJSON_AddItemToObject(ptr, "Item", cJSON_CreateNumber(10.0));
}

int main()
{
    cJSON *root = cJSON_CreateObject();
    cJSON *pt_PPC = cJSON_CreateObject();
    cJSON_AddItemToObject(root, "PowerPC", pt_PPC);
    myFunc(pt_PPC);
    cJSON_Delete(root);

    _CrtDumpMemoryLeaks();
}

I enabled memory leak debugging (Link to Article) and used _CrtDumpMemoryLeaks() to print the possible memory leaks if any (to the debug output window).

There were no memory leaks detected.

trenki
  • 7,133
  • 7
  • 49
  • 61
  • yeah i did not post the orignal code. This was just to give an idea of what i was doing. My code is pretty large. Let me edit the code and give you more detail of what i am doing. – Modasser Sep 10 '15 at 15:56
0

Your code is not complete and has syntax errors so it's not possible to tell where the problem exactly is by looking at it.

Anyway it's most likely you're creating a JSON object (somewhere) and forgetting to add it to the root object or any of its descendants. When the root object is deleted the un-connected elements are not deleted and leaks memory.

Hope this helps.

Paolo
  • 15,233
  • 27
  • 70
  • 91
  • Thnx for the advice. It is not possible for me to post the complete code. I will try to find such objects. – Modasser Sep 10 '15 at 16:24
  • 1
    Yes, it was not a complaint. I understand that extracting *the relevat parts of the code* is not always trivial. But assuming you're not leaking memory *outside* cJSON I'm quite confident the problem is an orphan object. – Paolo Sep 10 '15 at 16:27
0

I have found the problem. The problem is still somehow in the cJSON lib. Inside a function i am doing something like this.

uint8 *arr; arr = (uint8 *)malloc(t_DataVariableInfo.s32_Size); getvariables(&arr); // this function gets some variables from a datapool //pt_data is the pointer of cJSON where this number has to be added cJSON_AddItemToArray(pt_data,cJSON_CreateNumber(arr[i])); free(arr);

Now because of some reason arr could not be freed. The cJSON_CreateNumber() function is maybe doing some changes in it. I got it to work by doing this

uint8 *arr; arr = (uint8 *)malloc(t_DataVariableInfo.s32_Size); uint8 *address = arr; getvariables(&arr); // this function gets some variables from a datapool //pt_data is the pointer of cJSON where this number has to be added cJSON_AddItemToArray(pt_data,cJSON_CreateNumber(arr[i])); free(address);

Thanks for the help. Sorry i could not post all the code. Is this a problem of cJSON_CreateNumber()??

Modasser
  • 3
  • 4