我正在使用 cJSON library我有一个功能如下:
void printJsonObject(cJSON *item)
{
char *json_string = cJSON_Print(item);
printf("%s\n", json_string);
}
这个函数会不会泄漏内存?
最佳答案
我从未使用过 cJSON ,但根据此 link 中的函数定义, 看起来像
char *cJSON_Print(cJSON *item) {return print_value(item,0,1);}
和
static char *print_value(cJSON *item,int depth,int fmt);
来自 print_value()
函数,返回的指针由 cJSON_strdup()
分配[这是malloc()
的组合修改版|和 memcpy()
] 并将其返回给调用者。
因为我看不到跟踪分配的方法,IMO,分配的内存需要是 free()
d 调用函数。否则就是内存泄漏。
关于c - 调用者是否应该释放 cJSON_Print() 的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27394364/