First I dynamically allocate an object:
Object* someObject = new Object();
Then I insert the address of this pointer into a vector:
std::vector<Object**> someVector0;
someVector0.push_back(&someObject);
Then I insert the address of an address living in someVector0
to another vector:
std::vector<Object***> someVector1;
someVector1.push_back(&someVector0[0]);
Lastly, I insert the address of the aforementioned someVector1
to another vector:
std::vector<std::vector<Object***>*> someVector2;
someVector2.push_back(&someVector1);
Then I use the vector obtained, now I need to free the memory allocated:
//Do some stuff
//Free memory
for (int i = 0; i < someVector2.size(); i++) {
for (int j = 0; j < (*someVector2[i]).size(); j++) {
delete *(*(*someVector2[i])[j]);
delete *(*someVector2[i])[j];
delete (*someVector2[i])[j];
}
delete someVector2[i];
}
I had suspected that this block of code would have been enough to deallocate but, I ran into some problems whilst deallocating. I suspect that I might have dereferenced a nullptr
but I can't think of anything else, could there be a mistake in the way I delete the pointers?
Note: This is a lightened version of a problem I encountered with a program of a larger scale, none of the previously mentioned vectors is unnecessary and I do not want to handle any copies, do not change the types of the vectors.
EDIT:
Hi, after researching more and hearing out the comments I have come to my senses and even though I do not think there is a single soul that would find my code in any way sensible I strongly urge anyone that might see this as a valid way to do anything not to do it. As a comment already stated, my attempts at a solution showed that I did not have an idea of what I was doing, so please don't do it; it's practically unmaintainable. Instead, as another comment already pointed out use smart pointers or really be aware of what happens in your code when.