jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

terminate called after throwing an instance of 'Json::LogicError'

Open xuejun88 opened this issue 4 years ago • 2 comments

demo:

std::queue<Json::Value> _queue;

// thread1:
Json::Value root;
root["Name"] = "Lucy";
_queue.push(root);

// thread2:
Json::Value root =_queue.pop();
if(root["Name"].isString()){
    //....
}

root["Name"] cause core dump

terminate called after throwing an instance of 'Json::LogicError' what(): in Json::Value::resolveReference(key, end): requires objectValue

xuejun88 avatar Dec 15 '21 11:12 xuejun88

The demo is pretty incomplete @xuejun88. I can't tell if thread1 and thread2 are looping. I also can't tell whether thread2 is checking the _queue before performing a .pop.

You haven't mentioned how you're synchronizing access to the _queue among your threads here. std::queue is not a multi-thread-safe container. You have to supply some locking around it. Also, its pop() member returns void, so you're not giving an accurate picture here.

If you're mid-pop and another thread comes along an does a simultaneous push, I wouldn't expect this to be successful. I believe what you're getting is undefined behavior related to racing accesses to the _queue.

BillyDonahue avatar Dec 15 '21 23:12 BillyDonahue

我遇到了相同的问题,请问你有什么解决方案嘛

Remember-the-past avatar Aug 14 '23 09:08 Remember-the-past