mongodb中obj()使用問題

使用BSONObjBuilder或BSONArrayBuilder定義的變量,調用obj()時需要注意:

同一變量不能調用obj()兩次,第一次有效,第二次段錯誤

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

560         /**
561          * destructive
562          * The returned BSONObj will free the buffer when it is finished.
563          * @return owned BSONObj
564         */
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 NULL
571             return BSONObj(h);
572         }

雖然不知道decouple()是如何設置_b.buf()的,但請記住:調用decouple()後,_b.buf()爲NULL。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章