如何在每次调用后释放 json_object 指针
How to free json_object pointer after each call
我知道我们可以使用函数json_object_put()
来释放struct json_object
的指针。但是当我尝试在每次调用声明和使用它的函数后释放指向该结构的指针时,我遇到了问题。该程序将在 while 循环的第二个迭代器中为 Segmentation fault (core dumped)
,因为我在 test_json
函数中使用 json_object_put(obj1);
。
#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>
struct json_object * parse_object;
void init() {
char buffer_file[] = "{ \"object\": {\"array1\": [1, 2, 3], \"array2\": [4, 5, 6] } }";
parse_object = json_tokener_parse(buffer_file);
}
void test_json() {
struct json_object * obj1, * arr1;
json_object_object_get_ex(parse_object, "object", &obj1);
json_object_object_get_ex(obj1, "array1", &arr1);
int size = json_object_array_length(arr1);
printf("size = %d \n", size);
json_object_put(obj1);
}
int main(int argc, char const *argv[]) {
int j = 0;
int max = 4;
init();
while(++j < max) {
test_json();
}
json_object_put(parse_object);
return 0;
}
我可以在主函数中只使用 json_object_put(parse_object);
来释放所有 json_object
但是如果我将 max
值增加到很大(例如 1000000
),泄漏- 在我调用 json_object_put(parse_object);
.
之前,内存会变得非常多
那么,在这种情况下,如何释放json_object
(obj1
)(parse_object
总是一个全局变量)?如果不可能,请提供另一种解决方案,用于从 c.
中的 json 文件获取信息
Link 用于 struct json_object
的文档
你怎么知道有内存泄漏?
当我 运行 你的 valgrind 示例没有使用任何 json_object_put
时,堆摘要总是像下面这样(我 运行 你的代码带有 max = 4
和 max = 1000000
):
==57154== HEAP SUMMARY:
==57154== in use at exit: 2,949 bytes in 21 blocks
==57154== total heap usage: 32 allocs, 11 frees, 5,826 bytes allocated
==57154==
==57154== LEAK SUMMARY:
==57154== definitely lost: 0 bytes in 0 blocks
==57154== indirectly lost: 0 bytes in 0 blocks
==57154== possibly lost: 0 bytes in 0 blocks
==57154== still reachable: 2,949 bytes in 21 blocks
==57154== suppressed: 0 bytes in 0 blocks
==57154== Rerun with --leak-check=full to see details of leaked memory
当我 运行 示例仅使用 json_object_put(parse_object);
时,所有内存都被释放:
==57937== HEAP SUMMARY:
==57937== in use at exit: 0 bytes in 0 blocks
==57937== total heap usage: 32 allocs, 32 frees, 5,826 bytes allocated
==57937==
==57937== All heap blocks were freed -- no leaks are possible
注意:我用json-c repository
的master b运行ch测试过
此外 json-c documentation 指出 没有 引用计数将在调用 json_object_object_get_ex
时更改。
你用的是什么版本?您是否已检查 Github 上的问题?
根据example page on the GitHub Json-C project which documentation,我不得不承认,不是很清楚, 似乎
int json_object_put(struct json_object *jso);
当通过 json_tokener_parse()
调用获得 struct json_object
时,从不调用。当通过这些调用之一获得它时,它会被使用
jobj = json_object_new_object();
res = json_object_new_array();
这让我觉得
int test_json() {
struct json_object * obj1, * arr1;
json_object_object_get_ex(parse_object, "object", &obj1);
/* ... */
json_object_put(obj1);
return size;
}
在某种程度上,您使用 json_object_put(obj1);
释放了所有分配的内存,因为 obj1
是唯一一个被释放的对象并且 parse_object
.
中没有留下任何东西
我的假设是,通过 json_tokener_parse()
获得的所有对象中的 json_object_put(obj1);
,一个接一个地替代了对 json_object_put(parse_object);
的一次调用。
@mattefrank 的分析证实了这一假设。
我知道我们可以使用函数json_object_put()
来释放struct json_object
的指针。但是当我尝试在每次调用声明和使用它的函数后释放指向该结构的指针时,我遇到了问题。该程序将在 while 循环的第二个迭代器中为 Segmentation fault (core dumped)
,因为我在 test_json
函数中使用 json_object_put(obj1);
。
#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>
struct json_object * parse_object;
void init() {
char buffer_file[] = "{ \"object\": {\"array1\": [1, 2, 3], \"array2\": [4, 5, 6] } }";
parse_object = json_tokener_parse(buffer_file);
}
void test_json() {
struct json_object * obj1, * arr1;
json_object_object_get_ex(parse_object, "object", &obj1);
json_object_object_get_ex(obj1, "array1", &arr1);
int size = json_object_array_length(arr1);
printf("size = %d \n", size);
json_object_put(obj1);
}
int main(int argc, char const *argv[]) {
int j = 0;
int max = 4;
init();
while(++j < max) {
test_json();
}
json_object_put(parse_object);
return 0;
}
我可以在主函数中只使用 json_object_put(parse_object);
来释放所有 json_object
但是如果我将 max
值增加到很大(例如 1000000
),泄漏- 在我调用 json_object_put(parse_object);
.
那么,在这种情况下,如何释放json_object
(obj1
)(parse_object
总是一个全局变量)?如果不可能,请提供另一种解决方案,用于从 c.
Link 用于 struct json_object
的文档你怎么知道有内存泄漏?
当我 运行 你的 valgrind 示例没有使用任何 json_object_put
时,堆摘要总是像下面这样(我 运行 你的代码带有 max = 4
和 max = 1000000
):
==57154== HEAP SUMMARY:
==57154== in use at exit: 2,949 bytes in 21 blocks
==57154== total heap usage: 32 allocs, 11 frees, 5,826 bytes allocated
==57154==
==57154== LEAK SUMMARY:
==57154== definitely lost: 0 bytes in 0 blocks
==57154== indirectly lost: 0 bytes in 0 blocks
==57154== possibly lost: 0 bytes in 0 blocks
==57154== still reachable: 2,949 bytes in 21 blocks
==57154== suppressed: 0 bytes in 0 blocks
==57154== Rerun with --leak-check=full to see details of leaked memory
当我 运行 示例仅使用 json_object_put(parse_object);
时,所有内存都被释放:
==57937== HEAP SUMMARY:
==57937== in use at exit: 0 bytes in 0 blocks
==57937== total heap usage: 32 allocs, 32 frees, 5,826 bytes allocated
==57937==
==57937== All heap blocks were freed -- no leaks are possible
注意:我用json-c repository
的master b运行ch测试过此外 json-c documentation 指出 没有 引用计数将在调用 json_object_object_get_ex
时更改。
你用的是什么版本?您是否已检查 Github 上的问题?
根据example page on the GitHub Json-C project which documentation,我不得不承认,不是很清楚, 似乎
int json_object_put(struct json_object *jso);
当通过 json_tokener_parse()
调用获得 struct json_object
时,从不调用。当通过这些调用之一获得它时,它会被使用
jobj = json_object_new_object();
res = json_object_new_array();
这让我觉得
int test_json() {
struct json_object * obj1, * arr1;
json_object_object_get_ex(parse_object, "object", &obj1);
/* ... */
json_object_put(obj1);
return size;
}
在某种程度上,您使用 json_object_put(obj1);
释放了所有分配的内存,因为 obj1
是唯一一个被释放的对象并且 parse_object
.
我的假设是,通过 json_tokener_parse()
获得的所有对象中的 json_object_put(obj1);
,一个接一个地替代了对 json_object_put(parse_object);
的一次调用。
@mattefrank 的分析证实了这一假设。