[Boost-bugs] [Boost C++ Libraries] #11662: Parse Json

Subject: [Boost-bugs] [Boost C++ Libraries] #11662: Parse Json
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2015-09-17 04:31:50


#11662: Parse Json
------------------------+------------------------------
 Reporter: KigKrazy@… | Type: Bugs
   Status: new | Milestone: To Be Determined
Component: None | Version: Boost 1.58.0
 Severity: Problem | Keywords: Json,ptree
------------------------+------------------------------
 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_at_[hidden]\",\"framedipaddress\":\"10.10.1.3\"},{\"username\":\"a258055085_at_[hidden]\",\"framedipaddress\":\"10.10.1.4\"},{\"username\":\"77476848_at_[hidden]\",\"framedipaddress\":\"10.10.1.5\"},{\"username\":\"tyws0001_at_[hidden]\",\"framedipaddress\":\"10.10.1.10\"},{\"username\":\"zbr7066895_at_[hidden]\",\"framedipaddress\":\"10.10.1.9\"},{\"username\":\"1733064835_at_[hidden]\",\"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);

         }
 }

 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11662>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:19 UTC