[C++] delete on pointer doesn't call destructor?!

Started by
15 comments, last by streamer 15 years, 6 months ago
Quote:Original post by c4c0d3m0n
Universität Karlsruhe - Informatik

You should check if your University participates in the MSDNAA (Acadamic Alliance). Then you can get the full version of Visual Studio 2008 for free.
Advertisement
Quote:Original post by DevFred
Quote:Original post by c4c0d3m0n
Universität Karlsruhe - Informatik

You should check if your University participates in the MSDNAA (Acadamic Alliance). Then you can get the full version of Visual Studio 2008 for free.


Why, if he doesn't even use Windows?
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
And what about:
std::vector<R2e::State*>::iterator it = m_state_stack.end();
delete *it;
Quote:Original post by streamer
And what about:
std::vector<R2e::State*>::iterator it = m_state_stack.end();
delete *it;


Undefined behaviour.

std::vector<T>::end() returns a non-dereferenceable (but still comparable) iterator. std::vector<T>::back() is correct.
Quote:Original post by rip-off
Quote:Original post by streamer
And what about:
std::vector<R2e::State*>::iterator it = m_state_stack.end();
delete *it;


Undefined behaviour.

std::vector<T>::end() returns a non-dereferenceable (but still comparable) iterator. std::vector<T>::back() is correct.


Then if his code is correct, there must be a problem with desctructor.

Anyway the code can be defined as:
if ( !m_state_stack.empty() ) {       m_state_stack.back()->init( this );    }else{    m_state_stack.push_back( new T );    m_state_stack.back()->init( this );}or simplierif ( m_state_stack.empty() ) {      m_state_stack.push_back( new T );    }    m_state_stack.back()->init( this );


because you are deleting the last element, then pushing on the end
a new element.

You could just skip deleting the last element, and creating the new one, because there is already element on the end, and just initialize it with new values.

Maybe it is not possible, because I don't know what are you doing in Init function, but code looks cleaner.

Just some thoughts.
Quote:Original post by streamer
Quote:Original post by rip-off
Quote:Original post by streamer
And what about:
std::vector<R2e::State*>::iterator it = m_state_stack.end();
delete *it;


Undefined behaviour.

std::vector<T>::end() returns a non-dereferenceable (but still comparable) iterator. std::vector<T>::back() is correct.


Then if his code is correct, there must be a problem with desctructor.


Indeed, there was:
Quote:Original post by c4c0d3m0n
thanks emeyex! That was indeed the problem, I hadn't set a virtual destructor in the base class. I'm new to this inheritance thing. Thanks again!


Quote:Anyway the code can be defined as:
*** Source Snippet Removed ***

because you are deleting the last element, then pushing on the end
a new element.

You could just skip deleting the last element, and creating the new one, because there is already element on the end, and just initialize it with new values.

Maybe it is not possible, because I don't know what are you doing in Init function, but code looks cleaner.


It's not possible, not because of whatever he's doing in Init, but because he's using polymorphism. Notice how both times he uses new, he isn't doing, "push_back( new R2e::State(...) );", he's doing "push_back( new T(...) );", where T is a template parameter. That type is going to change around.
Shame on me. Didn't thought about that [sad]

This topic is closed to new replies.

Advertisement