keeping track of pointers to objects with STL
-
I have an stl list of pointers to objects. When I need to insert an object to the list, I new it and push_back its pointer. When the list is not needed any more, I iterate through the entire list and I delete each pointer. First question: Is there a more elegant way to do this or is my implementation ok? Sometimes I wish to copy a pointer for one list into another list, meaning that the two lists will contain a pointer to the same object. When the first list is destroyed, I delete all pointers inside it, as mentioned above. When the second list is destroyed, I do the same, however my pointers are already deleted and this of course crashes the program. My first thought was to implement reference counting on my object, however I am unsure of where to increment the reference. The code looks something like this: std::list list1; std::list list2; CMyObj* pOBJ = new CMyObj; list1.push_back(pOBJ); list2.insert(list2.begin(), list1.begin(), list1.end()); // delete all elements in list1 // delete all elements in list2 (crash) When list2.insert is called, a copy of the pointer from list1 is inserted into list2. I will need to increment the reference of the pointer which is inserted, however I am unsure of how to do this. Does anyone have a solution to this? Thanks in advance, Jeremy Pullicino C++ Developer Homepage
-
I have an stl list of pointers to objects. When I need to insert an object to the list, I new it and push_back its pointer. When the list is not needed any more, I iterate through the entire list and I delete each pointer. First question: Is there a more elegant way to do this or is my implementation ok? Sometimes I wish to copy a pointer for one list into another list, meaning that the two lists will contain a pointer to the same object. When the first list is destroyed, I delete all pointers inside it, as mentioned above. When the second list is destroyed, I do the same, however my pointers are already deleted and this of course crashes the program. My first thought was to implement reference counting on my object, however I am unsure of where to increment the reference. The code looks something like this: std::list list1; std::list list2; CMyObj* pOBJ = new CMyObj; list1.push_back(pOBJ); list2.insert(list2.begin(), list1.begin(), list1.end()); // delete all elements in list1 // delete all elements in list2 (crash) When list2.insert is called, a copy of the pointer from list1 is inserted into list2. I will need to increment the reference of the pointer which is inserted, however I am unsure of how to do this. Does anyone have a solution to this? Thanks in advance, Jeremy Pullicino C++ Developer Homepage
Hi Jeremy, As you correctly point out, some sort of ref counting is needed to address your problem. IMHO providing
CMyObj
with the ref counting machinery is not a good idea as this artifact properly does not belong toCMyObj
semantics, but rather it is imposed by external conditions (the necessity of storingCMyObj
pointers into several containers). A much cleaner approach is to use some sort of smart pointer that does the ref counting to the objects pointed to and deletes them when the objects are no further referenced. If you feel comfortable with Boost, check Boost Smart Pointers Library[^]: in particular,boost::shared_ptr
does exactly what you need. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
I have an stl list of pointers to objects. When I need to insert an object to the list, I new it and push_back its pointer. When the list is not needed any more, I iterate through the entire list and I delete each pointer. First question: Is there a more elegant way to do this or is my implementation ok? Sometimes I wish to copy a pointer for one list into another list, meaning that the two lists will contain a pointer to the same object. When the first list is destroyed, I delete all pointers inside it, as mentioned above. When the second list is destroyed, I do the same, however my pointers are already deleted and this of course crashes the program. My first thought was to implement reference counting on my object, however I am unsure of where to increment the reference. The code looks something like this: std::list list1; std::list list2; CMyObj* pOBJ = new CMyObj; list1.push_back(pOBJ); list2.insert(list2.begin(), list1.begin(), list1.end()); // delete all elements in list1 // delete all elements in list2 (crash) When list2.insert is called, a copy of the pointer from list1 is inserted into list2. I will need to increment the reference of the pointer which is inserted, however I am unsure of how to do this. Does anyone have a solution to this? Thanks in advance, Jeremy Pullicino C++ Developer Homepage
I highly recommend Loki::SmartPtr[^] for that purpose. Or if you don't feel comfortable with this, use boost::shared_ptr.