mongodb中obj()使用问题

来源:互联网 发布:淘宝预售如何合并付款 编辑:程序博客网 时间:2024/06/11 05:42

使用BSONObjBuilder或BSONArrayBuilder定义的变量,调用obj()时需要注意:

同一变量不能调用obj()两次,第一次有效,第二次段错误

请看src/mongo/bson/bsonobjbuilder.h中560-572行:

560         /**561          * destructive562          * The returned BSONObj will free the buffer when it is finished.563          * @return owned BSONObj564         */565         BSONObj obj() {566             bool own = owned();567             massert( 10335 , "builder does not own memory", own );568             doneFast();569             BSONObj::Holder* h = (BSONObj::Holder*)_b.buf();570             decouple(); // sets _b.buf() to NULL571             return BSONObj(h);572         }

虽然不知道decouple()是如何设置_b.buf()的,但请记住:调用decouple()后,_b.buf()为NULL。


0 0
原创粉丝点击