Why deleting a pointer to a vector of pointers causes the pointers to be not valid?

c++, delete-operator, destructor

Solution

The crash you get is completely unrelated; you declared `tellSomething` as returning a `std::string`, but you never return anything, so you go into undefined behavior-land; the fact that the program crashed after deallocating the vector is sheer luck, it could have crashed even at the first call to `tellSomething`.

Fixing that problem makes your program run fine (although you are leaking `a1` and `a2`).

By the way, this teaches you to turn up all the warnings: with `-Wall` that code would have given you an explicit warning about this potential problem:

matteo@teolapmint ~/cpp $ g++ -Wall testwarns.cpp 
testwarns.cpp: In member function ‘std::string A::tellSomething()’:
testwarns.cpp:12:5: warning: no return statement in function returning non-void [-Wreturn-type]

(just for the record: personally I recommend to compile with `-Wall -Wextra -ansi -pedantic`, often one warning can save you a lot of debug time).

Problem

EDIT: thank you for the answers! I declared the `tellSomething` method with a `std::string` return type while it should have been `void`! I was tripping myself up and blamed the poor guiltless `delete` operator :)! Let's consider a pointer to a dynamically allocated `vector` which contains pointers to dynamically allocated objects: ``` // Create the vector of pointers std::vector<A *>* v = new std::vector<A *>; // Create two objects A *a1 = new A; A *a2 = new A; // Populate the vector v->push_back(a1); v->push_back(a2); // Delete the vector delete v; // Try accessing one of the objects a1->tellSomething(); --> // Segmentation fault ``` As expected, if I delete the `vector`, the `delete` on the contained objects is not called (I also verified that `A::~A()` is never called in the above code), however, the last instruction gives a segmentation fault. What I expect from the `delete v` is two things: - The destructor for every contained object is called - The container is deallocated But in this case the contained objects are pointers, so no destructor is called. Also, `a1` is not `NULL` at the end of the listing. So, why the segmentation fault? Complete example here: http://ideone.com/r8YC0. Note: I don't usually use raw pointers with STL containers, please, consider this code as a purely theoretic example to help me understanding the logic of the `delete v` instruction.

Original source