I made a cute generic (i.e. template) List
class to handle lists in C++. The reason for that is that I found the std::list
class terribly ugly for everyday use and since I constantly use lists, I needed a new one. The major improvement is that with my class, I can use []
to get items from it. Also, still to be implemented is an IComparer
system to sort things.
I'm using this List
class in OBJLoader
, my class that loads Wavefront .obj files and converts them to meshes. OBJLoader
contains lists of pointers to the following "types": 3D positions, 3D normals, uv texture coordinates, vertices, faces and meshes. The vertices list has objects that must be linked to some objects in all of the 3D positions, 3D normals and uv texture coordinates lists. Faces link to vertices and meshes link to faces. So they are all inter-connected.
For the sake of simplicity, let's consider that, in some context, there are just two lists of pointers: List<Person*>
and List<Place*>
. Person
class contains, among others, the field List<Place*> placesVisited
and the Place
class contains the field List<Person*> peopleThatVisited
. So we have the structure:
class Person
{
...
public:
Place* placeVisited;
...
};
class Place
{
...
public:
List<People*> peopleThatVisited;
};
Now we have the following code:
Person* psn1 = new Person();
Person* psn2 = new Person();
Place* plc1 = new Place();
Place* plc2 = new Place();
Place* plc2 = new Place();
// make some links between them here:
psn1->placesVisited.Add(plc1, plc2);
psn2->placesVisited.Add(plc2, plc3);
// add the links to the places as well
plc1->peopleThatVisited.Add(psn1);
plc2->peopleThatVisited.Add(psn1, psn2);
plc3->peopleThatVisited.Add(plc3);
// to make things worse:
List<Person*> allThePeopleAvailable;
allThePeopleAvailable.Add(psn1);
allThePeopleAvailable.Add(psn2);
List<Place*> allThePlacesAvailable;
allThePlacesAvailable.Add(plc1);
allThePlacesAvailable.Add(plc2);
allThePlacesAvailable.Add(plc3);
All done. What happens when we reach }
? All the dtors are called and the program crashes because it tries to delete things two or more times.
The dtor of my list looks like this:
~List(void)
{
cursor = begin;
cursorPos = 0;
while(cursorPos < capacity - 1)
{
cursor = cursor->next;
cursorPos++;
delete cursor->prev;
}
delete cursor;
}
where Elem
is:
struct Elem
{
public:
Elem* prev;
T value;
Elem* next;
};
and T
is the generic List
type.
Which brings us back to the question: What ways are there to safely delete my List
classes? The elements inside may or may not be pointers and, if they are pointers, I would like to be able, when I delete my List
, to specify whether I want to delete the elements inside or just the Elem
wrappers around them.
Smart pointers could be an answer, but that would mean that I can't have a List<bubuType*>
, but just List<smart_pointer_to_bubuType>
. This could be ok, but again: declaring a List<bubuType*>
would cause no error or warning and in some cases the smart pointers would cause some problems in the implementation: for example, I might want to declare a List<PSTR>
for some WinAPI returns. I think getting those PSTR
inside smart pointers would be an ugly job. Thus, the solution I'm looking for I think should be somehow related to the deallocation system of the List
template.
Any ideas?
The best answer is that you must think on the lifetime of each one of the objects, and the responsibility of managing that lifetime.
In particular, in a relation from people and the sites they have visited, most probably neither of them should be naturally made responsible for the lifetime of the others: people can live independently from the sites that they have visited, and places exists regardless of whether they have been visited. This seems to hint that the lifetime of both people and sites is unrelated to the others, and that the pointers held are not related to resource management, but are rather references (not in the C++ sense).
Once you know who is responsible for managing the resource, that should be the code that should delete
(or better hold the resource in a container or suitable smart pointer if it needs to be dynamically allocated), and you must ensure that the deletion does not happen before the other objects that refer to the same elements finish with them.
If at the end of the day, in your design ownership is not clear, you can fall back to using shared_ptr
(either boost
or std
) being careful not to create circular dependencies that would produce memory leaks. Again to use the shared_ptr
s correctly you have to go back and think, think on the lifetime of the objects...