Susan Yanders
Susan Yanders

Reputation: 874

C++ unique_ptr not calling the destructor

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();

Upvotes: 7

Views: 6992

Answers (1)

Susan Yanders
Susan Yanders

Reputation: 874

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.

Upvotes: 17

Related Questions