Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ unique_ptr not calling the destructor

Tags:

c++

pointers

I have a vector of unique_ptrs that point to a class called state. When I call pop_back() with the vector, the unique pointer is removed from memory (I think), but the state object that it pointed to never gets deleted. Either that or the unique pointer somehow doesn't call the destructor when deleting the object it points to? All I know is that my destructor doesn't get called when my unique pointer is removed from the vector.

This is the vector:

std::vector< std::unique_ptr<State> > mStates;

I tried:

mStates.pop_back();

That removes the unique pointer, and I thought the unique pointer would delete the state for me and call the state's destructor, but that didn't happen. BTW I add elements by using:

 mStates.push_back();
like image 678
Susan Yanders Avatar asked Aug 20 '13 21:08

Susan Yanders


1 Answers

I fixed it. My problem was that I needed to have a virtual destructor. I didn't even consider that a virtual destructor could exist. It works now.

like image 158
Susan Yanders Avatar answered Oct 09 '22 10:10

Susan Yanders