[CXX-665] segment fault with vector Created: 17/Sep/15  Updated: 11/Sep/19  Resolved: 18/Sep/15

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Better Wang Assignee: Unassigned
Resolution: Done Votes: 0
Labels: legacy-cxx
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

centos 5.5
boost1.55
gcc4.8.2
mongo-cxx-driver(legacy)



 Description   

Hi
First,I introduce the installtion of mongo-cxx.My centos has gcc4.8.2 and gcc4.4.6, but I install the boost1.55 and mongo-cxx with gcc4.8.2. And it's fine.
I have a function to get query results and push them into a vector(vec). But when I get the data from the vec, segment fault occurs. code is :

        cursor = conn->query(dbco , obj);
	vector<BSONObj> vObj;
	while(cursor->more()){
		vObj.push_back(cursor->next());
	}
        BSONObj temp = vObj[0];// vObj.size()>0
        cout << temp.objsize()<<endl;

gdb debug info is :

(gdb) bt
#0  0x00007ffff6432e36 in ?? ()
#1  0x000000000040b460 in mongo::ConstDataView const& mongo::ConstDataView::readNative<int>(int*, unsigned long) const ()
#2  0x000000000040a491 in int mongo::ConstDataView::readNative<int>(unsigned long) const ()
#3  0x0000000000409627 in int mongo::ConstDataView::readLE<int>(unsigned long) const ()
#4  0x0000000000408d3d in mongo::BSONObj::objsize() const ()

anyone know this?

the some code is fine with my mac.



 Comments   
Comment by Andrew Morrow (Inactive) [ 18/Sep/15 ]

Hi better62932 - Your question is better suited to the mongodb-user mailing list: https://groups.google.com/forum/#!forum/mongodb-user

I recommend you post your question there.

I'm closing this ticket as there is no bug to be fixed in the driver, but please feel free to re-open it if you need additional help with the lifetime issues.

Comment by Better Wang [ 18/Sep/15 ]

Hi
Thank you! The problem has solved.
Another doubt is that what is throughput of mongo cxx driver. I wang to query data meeting some conditions and then update them 。It costs several seconds to query about 1000 datas and update them(use query method multi parameter). Can I speed up efficiency to more than 10000/s use other method. By the way , I index the queried conditions.

Comment by Andrew Morrow (Inactive) [ 17/Sep/15 ]

The issue here is that the BSONObj returned from DBClientCursor::next() may by invalidated by subsequent calls to DBClientCursor::next or by destroying the cursor. If you want to capture a long lived copy of the returned BSONObj, you should write it as follows:

        cursor = conn->query(dbco , obj);
	vector<BSONObj> vObj;
	while(cursor->more()){
		vObj.push_back(cursor->next().getOwned());
	}
        BSONObj temp = vObj[0];// vObj.size()>0
        cout << temp.objsize()<<endl;

The call to getOwned will make a copy of the BSONObj that will not be invalidated by subsequent use of the cursor.

Generated at Wed Feb 07 21:59:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.