gpt4 book ai didi

c - Json-Glib 对象插入时跳转无效

转载 作者:行者123 更新时间:2023-11-30 15:20:18 26 4
gpt4 key购买 nike

我正在使用 C 语言 Json 库,即 Json-Glib。问题是图书馆一直对我进行段错误,我不明白为什么。我阅读了所有文档,并了解到,根节点可以保存一些原始值,但更重要的是一个对象。我正在尝试实现一个对象串联,但是当我尝试将一个对象放入节点中,甚至只是在其中添加字符串成员时,我会得到无效跳转,因为通常大小 8(字符串)的读取无效。下面的代码尝试通过在检索到的 JsonObject 中插入少量字符串来实现从 JsonNode 检索 JsonObject。

int main(){

const gchar *auth = "authentication";

//here i initialize my Root JsonNode with an object in it and a JsonObject to be able to get the object from the JsonNode
JsonObject* authObject = json_object_new();
JsonNode* extNode = json_node_new(JSON_NODE_OBJECT);

// Here i retrieve the initialized JsonObject that is inside my JsonNode
authObject = json_node_get_object(extNode);

// And here some few insertion of strings in the object
SegFault Here -> json_object_set_string_member(authObject, "action", "authenticate");
json_object_set_string_member(authObject, "type", "authType");
json_object_set_string_member(authObject, "resource", "resource");
json_object_set_string_member(authObject, "version", "none");
json_object_set_string_member(authObject, "data", "loginData");

//here i try to print my json file but i can't even reach this execution line due to SegFault
char* toto = cometd_json_node2str(extNode);
puts(toto);

return 0;
}

这是 Valgrind 报告:

==4910== Memcheck, a memory error detector
==4910== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4910== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==4910== Command: ./a.out
==4910==
==4910== Invalid read of size 8
==4910== at 0x10013C28D: json_object_set_string_member (in /usr/local/Cellar/json-glib/1.0.2/lib/libjson-glib-1.0.0.dylib)
==4910== by 0x100000DD1: main (main.c:50)
==4910== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4910==
==4910==
==4910== Process terminating with default action of signal 11 (SIGSEGV)
==4910== Access not within mapped region at address 0x0
==4910== at 0x10013C28D: json_object_set_string_member (in /usr/local/Cellar/json-glib/1.0.2/lib/libjson-glib-1.0.0.dylib)
==4910== by 0x100000DD1: main (main.c:50)
==4910== If you believe this happened as a result of a stack
==4910== overflow in your program's main thread (unlikely but
==4910== possible), you can try to increase the size of the
==4910== main thread stack using the --main-stacksize= flag.
==4910== The main thread stack size used in this run was 8388608.
==4910==
==4910== HEAP SUMMARY:
==4910== in use at exit: 836,054 bytes in 2,098 blocks
==4910== total heap usage: 2,577 allocs, 479 frees, 1,410,814 bytes allocated
==4910==
==4910== LEAK SUMMARY:
==4910== definitely lost: 3,167 bytes in 45 blocks
==4910== indirectly lost: 5,357 bytes in 22 blocks
==4910== possibly lost: 20,836 bytes in 214 blocks
==4910== still reachable: 128,596 bytes in 853 blocks
==4910== suppressed: 678,098 bytes in 964 blocks
==4910== Rerun with --leak-check=full to see details of leaked memory
==4910==
==4910== For counts of detected and suppressed errors, rerun with: -v
==4910== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 1 from 1)

任何帮助都会非常感激,事实上,如果我可以调试这个,我将编写一个关于 Json-Glib 的教程来帮助其他程序员不要为此苦苦挣扎。

最佳答案

您正在覆盖 authObject 指针:

JsonObject* authObject = json_object_new(); // ← You create it here…
JsonNode* extNode = json_node_new(JSON_NODE_OBJECT);

authObject = json_node_get_object(extNode); // ← … but overwrite it here

在新创建的 JsonNode 上调用 json_node_get_object() 将返回 NULL,因为您尚未设置该对象。

如果您想使用对象设置 JsonNode,请改用 json_node_init_object():

JsonObject *authObject = json_object_new ();
JsonNode *node = json_node_init_object (json_node_alloc (), authObject);

或者使用json_node_new()json_node_take_object():

JsonObject *authObject = json_object_new ();
JsonNode *node = json_node_new (JSON_NODE_OBJECT);
json_node_take_object (node, authObject);

现在您可以将成员添加到authObject

关于c - Json-Glib 对象插入时跳转无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30097335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com