Boost:multi-index Question on usage of project & modify_key

Hello. I am using projection technique & modify_key in one of my member functions: Assuming I have the following data in the container: ObjId ObjIp ObjIndex pMyObj ------------------------------------------------------------------------------------- ID1 10.10.1.2 1 0x10239032 ID2 10.10.1.3 2 0x23423423 ID3 10.10.1.4 3 0x23132423 The member function 'UpdateKeyObjId' accepts the value on column 3 as key & changes the corresponding value for the field in Column 1. My code seems to work fine for all rows except the last row. When I use modify key on the last record, the app crashes at the highlighted line. I am not sure if my usage of project & modify_key right, could someone please point out if there are any errors in the usage? Thanks /R struct MyMap { std::string ObjId; unsigned long ObjIp; unsigned long ObjIndex; MyObj *pMyObj; MyMap(std::string param_ObjId, unsigned long param_ObjIp, unsigned long param_ObjIndex, MyObj* param_pMyObj): ObjId(param_ObjId), ObjIp(param_ObjIp), ObjIndex(param_ObjIndex), pMyObj(param_pMyObj) {} ~MyMap(){ ObjId.erase(); ObjIp = 0; ObjIndex = 0; pMyObj = 0; } }; struct ObjId{}; struct ObjIp{}; struct ObjIndex{}; typedef multi_index_container< MyMap, indexed_by< ordered_unique< tag <ObjId>, BOOST_MULTI_INDEX_MEMBER(MyMap,std::string,ObjId)>, ordered_non_unique< tag<ObjIp>,BOOST_MULTI_INDEX_MEMBER(MyMap,unsigned long,ObjIp)>, ordered_unique< tag<ObjIndex>,BOOST_MULTI_INDEX_MEMBER(MyMap,unsigned long,ObjIndex)> >
tMyObjMap;
class MyObjContainer{ private: tMyObjMap MyContainer; public: UpdateKeyObjId(unsigned long, std::string); }; bool MyObjContainer::UpdateKeyObjId(unsigned long param_ObjIndex, std::string param_ObjId) { typedef tMyObjMap::index<ObjIndex>::type tMyObjMapByIndex; tMyObjMapByIndex& ObjIndex_index = MyContainer.get<ObjIndex>(); tMyObjMap::index_iterator<ObjIndex>::type it = ObjIndex_index.find(param_ObjIndex); typedef tMyObjMap::index<ObjId>::type tMyObjMapById; tMyObjMapById::iterator ObjIdit = MyContainer.project<0>(it); // Use the modify_key method to update the ObjIp key with new value return MyContainer.get<0>().modify_key(ObjIdit, ll::_1 = param_ObjId); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ }

Ramesh escribió:
Hello.
I am using projection technique & modify_key in one of my member functions:
Assuming I have the following data in the container:
ObjId ObjIp ObjIndex pMyObj ------------------------------------------------------------------------------------- ID1 10.10.1.2 1 0x10239032 ID2 10.10.1.3 2 0x23423423 ID3 10.10.1.4 3 0x23132423
The member function 'UpdateKeyObjId' accepts the value on column 3 as key & changes the corresponding value for the field in Column 1.
My code seems to work fine for all rows except the last row. When I use modify key on the last record, the app crashes at the highlighted line. I am not sure if my usage of project & modify_key right, could someone please point out if there are any errors in the usage?
Hello Ramesh, At first sight your code appears correct. The only plausible way that I can figure out the offending statement can crash: return MyContainer.get<0>().modify_key(ObjIdit, ll::_1 = param_ObjId); happens if ObjIdit==MyContainer.end(). You can check this by placing an assertion before that: typedef tMyObjMap::index<ObjIndex>::type tMyObjMapByIndex; tMyObjMapByIndex& ObjIndex_index = MyContainer.get<ObjIndex>(); tMyObjMap::index_iterator<ObjIndex>::type it = ObjIndex_index.find(param_ObjIndex); assert(it!=ObjIndex_index.end()); ... If the assertion fails then there's simply no element with the given param_ObjIndex, which I guess you can trace back to some problem elsewhere in your program. HTH, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

Thank you, I wasnt checking the return from find, The assert helped me
locate the actual problem which was in another call.
On Wed, Jan 14, 2009 at 11:30 PM,
Ramesh escribió:
Hello.
I am using projection technique & modify_key in one of my member functions:
Assuming I have the following data in the container:
ObjId ObjIp ObjIndex pMyObj
------------------------------------------------------------------------------------- ID1 10.10.1.2 1 0x10239032 ID2 10.10.1.3 2 0x23423423 ID3 10.10.1.4 3 0x23132423
The member function 'UpdateKeyObjId' accepts the value on column 3 as key & changes the corresponding value for the field in Column 1.
My code seems to work fine for all rows except the last row. When I use modify key on the last record, the app crashes at the highlighted line. I am not sure if my usage of project & modify_key right, could someone please point out if there are any errors in the usage?
Hello Ramesh,
At first sight your code appears correct. The only plausible way that I can figure out the offending statement can crash:
return MyContainer.get<0>().modify_key(ObjIdit, ll::_1 = param_ObjId);
happens if ObjIdit==MyContainer.end(). You can check this by placing an assertion before that:
typedef tMyObjMap::index<ObjIndex>::type tMyObjMapByIndex; tMyObjMapByIndex& ObjIndex_index = MyContainer.get<ObjIndex>();
tMyObjMap::index_iterator<ObjIndex>::type it = ObjIndex_index.find(param_ObjIndex);
assert(it!=ObjIndex_index.end());
...
If the assertion fails then there's simply no element with the given param_ObjIndex, which I guess you can trace back to some problem elsewhere in your program.
HTH,
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
joaquin@tid.es
-
Ramesh