I have 2 classes using CRTP. Class A parses a json file. Class B gets the Json data by calling get() method of Class A. Class A in turn calls set() of Class B after parsing the Json.
template <class T>
class A
{
private:
nlohmann::json doc;
using jptr = nlohmann::json::json_pointer;
jptr addl_fld_ptr;
public:
parse_nf_event(std::string event)
{
try
{
doc = nlohmann::json::parse(event);
}
catch(const nlohmann::json::exception& e)
{
std::cout<<"MVRP: Cannot create Json from string\n";
throw "parse_nf_event: json string invalid";
}
addl_fld_ptr="/event/path/to/msuid/"_json_pointer;
}
inline void get_msuid()
{
//return "NULL" if field not present
static_cast<T&>(*this).set_msuid(doc.value(addl_fld_ptr/"msuid","NULL"));
}
};
class B:public A<B>
{
private:
something *ptr;
public:
B(std::string str):A(str)//str is json string
{
}
inline void set_msuid(std::string val)
{
ptr->msu_id.assign(val);
}
void* get_payload()
{
ptr = new something();
get_msuid();
return ptr;
}
};
Above code is trimmed version
User calls get_payload.
Sometimes I am getting a core
(gdb) bt
#0 0x00007fc2a27cc38d in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x00000000006be6ff in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign (__str="",
this=<optimized out>) at /usr/local/include/c++/5.4.0/bits/basic_string.h:1095
#2 B::set_msuid (this=0x7fc298ff6b20,
val=<error reading variable: access outside bounds of object referenced via synthetic pointer>)
#3 A<B>::get_msuid (this=0x7fc298ff6b20)
#4 B::get_payload (this=0x7fc298ff6b20)
This is the first time I am using CRTP, and not sure if I did it right. I am not able to understand the reason from back trace. From the Json I can see msuid=""
so nlohmann parser returns empty string.
Aucun commentaire:
Enregistrer un commentaire