I've been working on a Qt project and I encountered a problem with
deleting objects that are being hold in a map. I prepared a simple C++ code to show my
problem:
#include
#include
After
closing the program I get:
Debug Assertion
Failed!
_BLOCK_TYPE_IS_VALID
etc..
My
question is: why is that? The reason is probably that map gets deleted after I quit the
program and it holds an A object (a) that is being deleted before I close the program.
However, I passed a value, not an address, so what's the
problem?
Isn't that value just copied into the
map and held in some different address?
Yes, you're
double-deleting.
Maps copy their elements, and
your type has no copy constructor. You're actually copying the
A
yourself with mapa[name] = *a
anyway.
The ownership of the member pointer is
also very unclear.
Abide by the rule of three.
Ideally avoid the manual dynamic allocation altogether; A
should have a std::vector
instead.
No comments:
Post a Comment