JSONCPP - 声明JSON :: Reader时的段错误

时间:2016-04-01 19:39:57

标签: jsoncpp

我有一个回调函数,只要它从套接字接收到东西就会被调用。然后它解析字符串并使用jsoncpp将其转换为JSON对象。

但是,我有一个奇怪的段错误。代码看起来像这样

void LibEventClient::on_read(int fd, short ev)
{
    char buf[1024];
    ....
    ....    // read stuff into buf

    Json::Value root;   // will contains the root value after parsing.
    Json::Reader reader;
    bool parsingSuccessful = reader.parse( std::string(buf), root );
}

但我在声明Json::Reader

的行上有段错误

回溯看起来像这样

Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=0x7ffff70df760 <main_arena>, bytes=512) at malloc.c:3489
 3489   malloc.c: No such file or directory.
(gdb) bt
#0  _int_malloc (av=0x7ffff70df760 <main_arena>, bytes=512) at malloc.c:3489
#1  0x00007ffff6da37b0 in __GI___libc_malloc (bytes=512) at malloc.c:2891
#2  0x00007ffff735adad in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7bb65c8 in __gnu_cxx::new_allocator<Json::Value*>::allocate (this=0x7fffffffdaa0, __n=64) at /usr/include/c++/4.8/ext/new_allocator.h:104
#4  0x00007ffff7bb54d3 in std::_Deque_base<Json::Value*, std::allocator<Json::Value*> >::_M_allocate_node (this=0x7fffffffdaa0)
at /usr/include/c++/4.8/bits/stl_deque.h:533 
#5  0x00007ffff7bb2ea3 in std::_Deque_base<Json::Value*, std::allocator<Json::Value*> >::_M_create_nodes (this=0x7fffffffdaa0, __nstart=0x927818, 
__nfinish=0x927820) at /usr/include/c++/4.8/bits/stl_deque.h:627
#6  0x00007ffff7baf36f in std::_Deque_base<Json::Value*, std::allocator<Json::Value*> >::_M_initialize_map (this=0x7fffffffdaa0, __num_elements=0)
at /usr/include/c++/4.8/bits/stl_deque.h:601
#7  0x00007ffff7baf550 in std::_Deque_base<Json::Value*, std::allocator<Json::Value*> >::_Deque_base(std::_Deque_base<Json::Value*, std::allocator<Json::Value*> >&&) (this=0x7fffffffdaa0, __x=<unknown type in /lib/libUtil.so, CU 0x0, DIE 0x308db>)
at /usr/include/c++/4.8/bits/stl_deque.h:471
#8  0x00007ffff7bac432 in std::deque<Json::Value*, std::allocator<Json::Value*> >::deque(std::deque<Json::Value*, std::allocator<Json::Value*> >&&) (
this=0x7fffffffdaa0, __x=<unknown type in /lib/libUtil.so, CU 0x0, DIE 0x29ea8>)
at /usr/include/c++/4.8/bits/stl_deque.h:856
#9  0x00007ffff7baa2a8 in std::stack<Json::Value*, std::deque<Json::Value*, std::allocator<Json::Value*> > >::stack(std::deque<Json::Value*, std::allocator<Json::Value*> >&&) (this=0x7fffffffdaa0, __c=<unknown type in /lib/libUtil.so, CU 0x0, DIE 0x29ea8>)
at /usr/include/c++/4.8/bits/stl_stack.h:139
#10 0x00007ffff7b93b0c in Json::Reader::Reader (this=0x7fffffffdaa0) at jsoncpp.cpp:279
#11 0x00000000004168ae in LibEventClient::on_read (this=0x9cd920, fd=18, ev=2) at BbgGateway.cpp:612
#12 0x0000000000416f3e in LibEventClient::invoke_on_read (fd=18, events=2, ctx=0x9cd920) at BbgGateway.cpp:724
#13 0x00007ffff760df24 in event_base_loop () from /usr/lib/x86_64-linux-gnu/libevent-2.0.so.5
#14 0x000000000041752e in BbgGateway::queryActionLibEvent (this=0x67e470) at BbgGateway.cpp:788
#15 0x0000000000411b4e in BbgGateway::run (this=0x67e470) at BbgGateway.cpp:178
#16 0x000000000044c727 in main (argc=2, argv=0x7fffffffe8e8) at client.cpp:68

任何人都有关于发生了什么的理论?

1 个答案:

答案 0 :(得分:2)

不确定这是否有帮助,但在此示例中: Using libCurl and JsonCpp to parse from https webserver

他宣布:

  

std :: unique_ptr httpData(new std :: string());

然后在向httpData存储数据或从httpData检索数据时使用httpData.get(),如:

  

curl_easy_setopt(curl,CURLOPT_WRITEDATA,httpData.get());

  

std :: cout&lt;&lt; &#34; HTTP数据是:\ n&#34; &LT;&LT; * httpData.get()&lt;&lt;的std :: ENDL;

将httpData的初始声明替换为:

  

std :: string * httpData(new std :: string());

并用httpData替换httpData.get()的所有实例,使所有指针赋值都相同。

现在就像魅力一样。可以cout&lt;&lt; httpData并使用jsonReader.parse进行解析。