cJSON memory leak

后端 未结 2 395
野性不改
野性不改 2021-01-14 12:25

I use cJSON in my program to convert my values to JSON and write it to file. Here is the example of my code:

void writeStructToFile(IOPipe this, struct struc         


        
相关标签:
2条回答
  • 2021-01-14 13:02

    Initially I thought that it might be FILE I/O's internal buffers. But these are flushed automatically when they become too big.

    The real leak is that cJSON_Print allocates memory: a char array. You must free this after you're done:

    char* text = cJSON_Print(jout);
    fprintf(this->outstream, "%s", text);
    free(text);  // As suggested by PaulPonomarev.
    
    cJSON_Delete(jout);
    
    0 讨论(0)
  • 2021-01-14 13:25

    For a char* allocated cJSON_Print, it is said to use cJSON_FreePrintBuffer.

    0 讨论(0)
提交回复
热议问题