cJSON memory leak

后端 未结 2 396
野性不改
野性不改 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);
    

提交回复
热议问题