It looks like this should work, given "bldr.obj()" in your working example calls decouple as well to transfer buffer ownership. Can you provide a stack trace for this? I will try to reproduce as well.
On Tuesday, November 13, 2012 12:28:28 PM UTC-5, khanalesb wrote:I am trying to use BSONObjBuilder as a serializer and decouple to avoid memcopy.
mongo::BSONObjBuilder bldr;
....................
....................
....................
#ifdef X
//THIS WORKS
mongo::BSONObj obj = bldr.obj();
zmq::message_t replyBody( obj.objsize() );
std::memcpy( replyBody.data(), obj.objdata(), obj.objsize() );
#else
//SEG FAILT
int len;
char* buf = bldr.decouple(len);
zmq::message_t replyBody( buf, len, zmqFree );
#endif
Do I have to call anything else before decouple? Can I use it at all?
Thanks.
I am trying to use BSONObjBuilder as a serializer and decouple to avoid memcopy.
mongo::BSONObjBuilder bldr;
....................
....................
....................
#ifdef X
//THIS WORKS
mongo::BSONObj obj = bldr.obj();
zmq::message_t replyBody( obj.objsize() );
std::memcpy( replyBody.data(), obj.objdata(), obj.objsize() );
#else
//SEG FAILT
int len;
char* buf = bldr.decouple(len);
zmq::message_t replyBody( buf, len, zmqFree );
#endif
Do I have to call anything else before decouple? Can I use it at all?
Thanks.
I am trying to use BSONObjBuilder as a serializer and decouple to avoid memcopy.
mongo::BSONObjBuilder bldr;
....................
....................
....................
#ifdef X
//THIS WORKS
mongo::BSONObj obj = bldr.obj();
zmq::message_t replyBody( obj.objsize() );
std::memcpy( replyBody.data(), obj.objdata(), obj.objsize() );
#else
//SEG FAILT
int len;
char* buf = bldr.decouple(len);
zmq::message_t replyBody( buf, len, zmqFree );
#endif
Do I have to call anything else before decouple? Can I use it at all?
Thanks.
Your original usage of the decouple method was correct. This is definitely a bug in the implementation of BSONObjBuilder. A ticket has been entered to fix:
https://jira.mongodb.org/browse/SERVER-7671
While waiting on a fix my suggestion would to use your original implementation (with the memcpy) if you can afford to do so or test an alternate solution very carefully. Please track the ticket above for status on the fix.
Thanks for your help in identifying this!
James