fully deallocating the memory of a std::vector container

c++, memory-management, stl, vector

Solution

Yes, it is correct, provided `mvMyObjectVector_ptr` has been allocated using `new`.

Additionally, `MyObject` needs to satisfy certain requirements before it can be used with `std::vector`.

The call to `clear()` is redundant and can be omitted.

Some likely reasons for the `SIGABRT` include:

- `mvMyObjectVector_ptr` hasn't been allocated using `new`;

- `MyObject` violates the Rule of Three;

- the class the contains the vector violates the Rule of Three.

Problem

From the vector docs it would appear that the proper way to completely deallocate a vector of values to which you have a class member pointer such as: ``` std::vector<MyObject>* mvMyObjectVector_ptr; ... //In the class constructor: mvMyObjectVector_ptr = new std::vector<MyObject>(); ``` would be to invoke the following, in order, in the class's destructor implementation ``` mvMyObjectVector_ptr->clear(); delete mvMyObjectVector_ptr; ``` However, this appears to be leading to SIGABRT 'pointer being freed was not allocated' errors. Is the above idiom the correct way to completely deallocate the memory held at the address pointed to by a pointer to a vector (if it is, I assume my errors are coming from something else)? If not, what is the correct way?

Original source