I have a class whose constructor is only called once, but its destructor is called three times.
void test_body()
{
std::cout << "----- Test Body -----" << "\n";
System system1;
Body body1({1,1,1}, {2,2,2}, 1, system1);
system1.add_body(body1);
std::cout << system1.get_bodies()[0].get_pos() << "\n";
}
body.hpp:
class Body {
private:
Vec3D pos_;
Vec3D vel_;
double mass_{ 1 };
System* system_{ nullptr };
public:
/*#### Constructors ####*/
Body() noexcept = default;
Body(Vec3D pos, Vec3D vel, double mass, System& system):
pos_(pos), vel_(vel), system_{&system}
{
if (mass <= 0)
throw std::runtime_error("Mass cannot be negative.");
mass_ = mass;
std::cout << "Constructed Body" << "\n";
}
~Body() {std::cout << "Destructed Body" << "\n";}
/*#### Copy/Move ####*/
Body(const Body &) =default;
Body & operator=(const Body &) =default;
Body(Body &&) =default;
Body & operator=(Body &&) =default;
system.hpp:
class System {
private:
std::vector<Body> bodies;
public:
[[nodiscard]] inline
std::vector<Body> get_bodies() const {return bodies;}
inline
void add_body(Body& body)
{
bodies.emplace_back(std::move(body));
}
};
output:
----- Test Body -----
Constructed Body
(1.000000, 1.000000, 1.000000)
Destructed Body
Destructed Body
Destructed Body
I understand that it has to do with system1.add_body(body1);
and std::cout << system1.get_bodies()[0].get_pos() << "\n";
but questions are :
- How can an object be destructed more times than it has been constructed ?
- Is this a performance loss (should I be worried about it on a larger scale) ? If so, how can I work my way around it ?
PS: On a more general manner, I'll be happy to receive advice on my code!
CodePudding user response:
How can an object be destructed more times than it has been constructed ?
It can't. You are simply not logging every constructor that is being called.
For instance, bodies.emplace_back(std::move(body))
constructs a new Body
object using the Body(Body&&)
move constructor, which you have default
'ed and are not logging.
And std::vector<Body> get_bodies() const
returns a copy of bodies
, thus has to make new Body
objects using the Body(const Body&)
copy constructor, which you have likewise also default
'ed and are not logging.
Try the following instead, and you will see a better picture of what is really happening:
class Body
{
private:
Vec3D pos_;
Vec3D vel_;
double mass_{ 1 };
System* system_{ nullptr };
public:
/*#### Constructors ####*/
//Body() noexcept = default;
Body() {
std::cout << "Default Constructor " << static_cast<void*>(this) << "\n";
}
Body(Vec3D pos, Vec3D vel, double mass, System& system)
: pos_(pos), vel_(vel), mass_(mass), system_(&system)
{
...
std::cout << "Conversion Constructor " << static_cast<void*>(this) << "\n";
}
~Body() {
std::cout << "Destructor " << static_cast<void*>(this) << "\n";
}
/*#### Copy/Move ####*/
//Body(const Body &) = default;
Body(const Body &src)
: pos_(src.pos_), vel_(src.vel_), mass_(src.mass_), system_(src.system_)
{
...
std::cout << "Copy Constructor " << static_cast<void*>(this) << "\n";
}
//Body(Body &&) = default;
Body(Body &&src)
: pos_(std::move(src.pos_)), vel_(std::move(src.vel_)), mass_(src.mass_), system_(src.system_)
...
src.mass_ = 0;
src.system_ = nullptr;
...
std::cout << "Move Constructor " << static_cast<void*>(this) << "\n";
}
//Body& operator=(const Body &) = default;
Body& operator=(const Body &rhs) {
if (&rhs != this) {
pos_ = rhs.pos_;
vel_ = rhs.vel_;
mass_ = rhs.mass_;
system_ = rhs.system_;
}
std::cout << "Copy Assignment " << static_cast<const void*>(&rhs) << " -> " static_cast<void*>(this) << "\n";
return *this;
}
//Body& operator=(Body &&) = default;
Body& operator=(Body &&rhs) {
pos_ = std::move(rhs.pos_);
vel_ = std::move(rhs.vel_);
mass_ = rhs.mass_; rhs.mass_ = 0;
system_ = rhs.system_; rhs.system_ = nullptr;
std::cout << "Move Assignment " << static_cast<void*>(&rhs) << " -> " static_cast<void*>(this) << "\n";
return *this;
}
...
};