While using msgpack::clone() I had pretty bad issues (std::bad_cast) when cloning any object that would be allocated to the zone.
I think I figured why:
-
When creating a aligned_zone_size_visitor, s will be copied to a private property m_size:
struct aligned_zone_size_visitor {
explicit aligned_zone_size_visitor(std::size_t s)
:m_size(s) {}
...
private:
std::size_t m_size;
};
https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L529-L600
-
Because the original reference to s is not retained, s will stay at its initial value and msgpack::aligned_zone_size() always returns 0:
inline std::size_t aligned_zone_size(msgpack::object const& obj) {
std::size_t s = 0;
aligned_zone_size_visitor vis(s);
msgpack::object_parser(obj).parse(vis);
return s;
}
https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L602-L607
-
msgpack::clone() only instantiate a new zone if msgpack::aligned_zone_size() > 0 so it will never create a new zone:
/// clone object
/**
* Clone (deep copy) object.
* The copied object is located on newly allocated zone.
* @param obj copy source object
*
* @return object_handle that holds deep copied object and zone.
*/
inline object_handle clone(msgpack::object const& obj) {
std::size_t size = msgpack::aligned_zone_size(obj);
msgpack::unique_ptr<msgpack::zone> z(size == 0 ? MSGPACK_NULLPTR : new msgpack::zone(size));
msgpack::object newobj = z.get() ? msgpack::object(obj, *z) : obj;
return object_handle(newobj, msgpack::move(z));
}
https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L609-L622
Is this an actual bug or a feature I misunderstood?
While using
msgpack::clone()I had pretty bad issues (std::bad_cast) when cloning any object that would be allocated to the zone.I think I figured why:
When creating a
aligned_zone_size_visitor,swill be copied to a private propertym_size:https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L529-L600
Because the original reference to
sis not retained,swill stay at its initial value andmsgpack::aligned_zone_size()always returns 0:https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L602-L607
msgpack::clone()only instantiate a new zone ifmsgpack::aligned_zone_size() > 0so it will never create a new zone:https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L609-L622
Is this an actual bug or a feature I misunderstood?