user3240688
user3240688

Reputation: 1327

JSONCPP - segfault when declaring JSON::Reader

I have a callback function that gets called whenever it receives something from a socket. It then parses the string and turns it into a JSON object using jsoncpp.

However, I'm having a strange segfault. The code looks like this

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 );
}

But I'm having segfault on the line the declares Json::Reader

The backtrace looks like this

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

Anybody have any theories on what's going on?

Upvotes: 0

Views: 1088

Answers (1)

spotrevoc
spotrevoc

Reputation: 21

Not sure if this helps, but in the example here: Using libCurl and JsonCpp to parse from https webserver

He declares:

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

and then later uses httpData.get() when storing or retrieving data to or from httpData, as in:

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

and

std::cout << "HTTP data was:\n" << *httpData.get() << std::endl;

Replace the initial declaration of httpData with:

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

and replace all instances of httpData.get() with httpData leaving all pointer assignments the same.

Works like a charm now. Can cout << httpData and parse with jsonReader.parse.

Upvotes: 2

Related Questions