Opened 7 years ago

Last modified 7 years ago

#11662 new Bugs

Parse Json

Reported by: KigKrazy@… Owned by: Sebastian Redl
Milestone: To Be Determined Component: property_tree
Version: Boost 1.58.0 Severity: Problem
Keywords: Json, ptree Cc:

Description

use Boost to Parse Json on linux! when I use it with multithreading ,the Program crash!!

my code: json_test.h

#ifndef UNIT_JSON_BUGTEST_H
#define UNIT_JSON_BUGTEST_H
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/thread/thread.hpp>
#include <sstream>
#include <map>

using namespace boost;
using namespace boost::property_tree;
using namespace std;
#define JSON_BUG "{\"onlineUser\":[{\"username\":\"131898@QQ.COM\",\"framedipaddress\":\"10.10.1.3\"},{\"username\":\"a258055085@163.com\",\"framedipaddress\":\"10.10.1.4\"},{\"username\":\"77476848@QQ.COM\",\"framedipaddress\":\"10.10.1.5\"},{\"username\":\"tyws0001@163.com\",\"framedipaddress\":\"10.10.1.10\"},{\"username\":\"zbr7066895@163.com\",\"framedipaddress\":\"10.10.1.9\"},{\"username\":\"1733064835@qq.com\",\"framedipaddress\":\"10.10.1.12\"}]}"


typedef multimap<string, string> onlineuser_map;
onlineuser_map get_online_user_map(const string strJson);
void unit_json_bugtest_main();
#endif

json_test.cpp

#include "unit_json_bugtest.h"

onlineuser_map get_online_user_map(const string strJson)
{
	onlineuser_map mymap;
	//  cout << strJson << endl;
	stringstream ssJson(strJson);
	ptree pt;

	try{
		read_json(ssJson, pt);
	}
	catch(...)
	{
		cout << "read json string error" <<endl;
		return mymap;
	}

	try{
		ptree ptInfoArray = pt.get_child("onlineUser");
		BOOST_FOREACH(ptree::value_type &v, ptInfoArray)
		{
			ptree ptChild;
			std::stringstream streamChild;
			write_json(streamChild, v.second);
			json_parser::read_json(streamChild, ptChild);
			mymap.insert(make_pair(ptChild.get<string>("username"), ptChild.get<string>("framedipaddress")));
		}
	}
	catch(...)
	{
		mymap.clear();
		cout << "read json string error" <<endl;
		return mymap;
	}
	return mymap;
}
int i;
void my_thread_fun(int itest)
{
	get_online_user_map(JSON_BUG);
	cout << "i'm OK!!: " << itest<<endl; 
}

void main()
{
	i = 0;
	while(1)
	{
		thread th(my_thread_fun,i);
		i++;
		usleep(10);

	}
}

Change History (1)

comment:1 by viboes, 7 years ago

Component: Noneproperty_tree
Owner: set to Sebastian Redl
Note: See TracTickets for help on using tickets.