I am using MsgPack
as part of a custom back-end I am creating for the SOCI
C++ database access API. Initially, some of my SOCI
classes had MsgPack::sbuffer
objects as member variables, but I ran into some problems in the destructor of my SOCI
object - I believe the problem relates to the way that SOCI
reference counts some of its objects and the memory that underlies the MsgPack
object being freed multiple times.
In an attempt to remedy this problem, I decide to replace the MsgPack::sbuffer
member variables with std::vector<char>
member variables, and use these to fill the MsgPack::sbuffer
s using the pack_raw_body
method. Unfortunately, I'm having trouble with this as well.
Please consider the following (pseudo-)code snippet...
msgpack::sbuffer buf1;
msgpack::packer<msgpack::sbuffer> bufPkr1(&buf1);
bufPkr1.pack_array(num);
for (int ndx = 0; ndx < num; ++ndx) {
bufPkr1.pack_array(3);
bufPkr1.pack(std::string("foo"));
bufPkr1.pack(std::string("bar"));
bufPrk1.pack(221);
}
std::vector<char> chrVct = std::vector<char>(buf1.size(), *buf1.data());
msgpack::unpacked unPkd1;
msgpack::unpack(&unPkd1, buf1.data(), buf1.size());
msgpack::object toStr1 = unPkd1.get();
std::cout << "MsgPack1: " << toStr1 << std::endl;
msgpack::sbuffer buf2;
msgpack::packer<msgpack::sbuffer> bufPkr2(&buf2);
bufPkr1.pack_raw(chrVct.size());
bufPkr1.pack_raw_body(chrVct.data(), chrVct.size());
msgpack::unpacked unPkd2;
msgpack::unpack(&unPkd2, buf2.data(), buf2.size());
msgpack::object toStr2 = unPkd2.get();
std::cout << "MsgPack2: " << toStr2 << std::endl;
Output...
MsgPack1: [["foo", "bar", 221], ["foo", "bar", 221], ["foo", "bar", 221],..., ["foo", "bar", 221]]
MsgPack2: ""
In general, I'm just having trouble figuring out how to work with the MsgPack objects and am finding the documentation/examples a little sparse. Any help that people can offer would be much appreciated!