object assignment
Hi guy,
I just do a object assignment, but it Segmentation, could you give me some suggestion?
code:
class XReader{ public: XReader(const msgpack::object& obj) { _object = obj; // segmentfault } msgpack::object _object; }
Hi @dawnlucky
I tested the following code:
#include <msgpack.hpp>
class XReader{
public:
XReader(const msgpack::object& obj) {
_object = obj; // segmentfault
}
msgpack::object _object;
};
int main() {
msgpack::object obj;
XReader xl(obj);
}
It works for me. Also I've checked it using valgrind. No memory violation found. I think that using valgrind to your program is helpful to find the problem.
sorry, should add two test code:
XReader(const msgpack::object& obj) { _object = obj; //seem _object is null, I test it, _object.type is 0 std::ostringstream os; os << _object; cout<<os.str(); //nothing }
But if I add -ltcmalloc, it works fine.
the raw code of mine:
FPReader(const msgpack::object& obj){ _object = obj; if(_object.type != msgpack::type::MAP) throw FPNN_ERROR_FMT(FpnnProtoError, "NOT a MAP object:%d", _object.type); }
It always throw exception, and object type is 0.
msgpack::object has two member variables: https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/object_fwd.hpp#L98
When operator=() called, they are just copied. It is shallow copy.
I'm not sure what kind of msgpack::object is passed to FPReader.
FPReader(const msgpack::object& obj){
// Could you insert some check code for obj?
// For example
std::cout << obj << std::endl;
_object = obj;
if(_object.type != msgpack::type::MAP) throw FPNN_ERROR_FMT(FpnnProtoError, "NOT a MAP object:%d", _object.type);
}
the object is get from this: const msgpack::object& FPReader::_find(const char* key){ if(_object.via.map.size > 0){ msgpack::object_kv* pkv = _object.via.map.ptr; msgpack::object_kv* pkv_end = _object.via.map.ptr + _object.via.map.size;
do{
if(pkv->key.type != msgpack::type::STR) throw FPNN_ERROR_FMT(FpnnProtoError, "Key type:%d", pkv->key.type);
if(!strncmp(pkv->key.via.str.ptr, key, pkv->key.via.str.size)) return pkv->val;
++pkv;
}while (pkv < pkv_end);
}
return nilObj;
}
But if I add tcmalloc or jemalloc, it works fine.
Still not resolved.