Physics2D/RigidBody2D: Cleanup and fix memory leak

This commit is contained in:
Lynix 2016-10-14 17:56:50 +02:00
parent 8c1b4527fd
commit 119436a642
2 changed files with 18 additions and 14 deletions

View File

@ -62,10 +62,10 @@ namespace Nz
RigidBody2D& operator=(RigidBody2D&& object); RigidBody2D& operator=(RigidBody2D&& object);
private: private:
void Destroy();
std::vector<cpShape*> m_shapes; std::vector<cpShape*> m_shapes;
Collider2DRef m_geom; Collider2DRef m_geom;
Vector3f m_forceAccumulator;
Vector3f m_torqueAccumulator;
cpBody* m_handle; cpBody* m_handle;
PhysWorld2D* m_world; PhysWorld2D* m_world;
float m_gravityFactor; float m_gravityFactor;

View File

@ -20,8 +20,6 @@ namespace Nz
RigidBody2D::RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom) : RigidBody2D::RigidBody2D(PhysWorld2D* world, float mass, Collider2DRef geom) :
m_geom(), m_geom(),
m_forceAccumulator(Vector3f::Zero()),
m_torqueAccumulator(Vector3f::Zero()),
m_world(world), m_world(world),
m_gravityFactor(1.f), m_gravityFactor(1.f),
m_mass(0.f) m_mass(0.f)
@ -38,8 +36,6 @@ namespace Nz
RigidBody2D::RigidBody2D(const RigidBody2D& object) : RigidBody2D::RigidBody2D(const RigidBody2D& object) :
m_geom(object.m_geom), m_geom(object.m_geom),
m_forceAccumulator(Vector3f::Zero()),
m_torqueAccumulator(Vector3f::Zero()),
m_world(object.m_world), m_world(object.m_world),
m_gravityFactor(object.m_gravityFactor), m_gravityFactor(object.m_gravityFactor),
m_mass(0.f) m_mass(0.f)
@ -57,8 +53,7 @@ namespace Nz
RigidBody2D::RigidBody2D(RigidBody2D&& object) : RigidBody2D::RigidBody2D(RigidBody2D&& object) :
m_geom(std::move(object.m_geom)), m_geom(std::move(object.m_geom)),
m_forceAccumulator(std::move(object.m_forceAccumulator)), m_shapes(std::move(object.m_shapes)),
m_torqueAccumulator(std::move(object.m_torqueAccumulator)),
m_handle(object.m_handle), m_handle(object.m_handle),
m_world(object.m_world), m_world(object.m_world),
m_gravityFactor(object.m_gravityFactor), m_gravityFactor(object.m_gravityFactor),
@ -69,8 +64,7 @@ namespace Nz
RigidBody2D::~RigidBody2D() RigidBody2D::~RigidBody2D()
{ {
if (m_handle) Destroy();
cpBodyFree(m_handle);
} }
void RigidBody2D::AddForce(const Vector2f& force, CoordSys coordSys) void RigidBody2D::AddForce(const Vector2f& force, CoordSys coordSys)
@ -185,7 +179,10 @@ namespace Nz
if (m_geom.Get() != geom) if (m_geom.Get() != geom)
{ {
for (cpShape* shape : m_shapes) for (cpShape* shape : m_shapes)
{
cpBodyRemoveShape(m_handle, shape); cpBodyRemoveShape(m_handle, shape);
cpShapeFree(shape);
}
if (geom) if (geom)
m_geom = geom; m_geom = geom;
@ -248,19 +245,26 @@ namespace Nz
RigidBody2D& RigidBody2D::operator=(RigidBody2D&& object) RigidBody2D& RigidBody2D::operator=(RigidBody2D&& object)
{ {
if (m_handle) Destroy();
cpBodyFree(m_handle);
m_handle = object.m_handle; m_handle = object.m_handle;
m_forceAccumulator = std::move(object.m_forceAccumulator);
m_geom = std::move(object.m_geom); m_geom = std::move(object.m_geom);
m_gravityFactor = object.m_gravityFactor; m_gravityFactor = object.m_gravityFactor;
m_mass = object.m_mass; m_mass = object.m_mass;
m_torqueAccumulator = std::move(object.m_torqueAccumulator); m_shapes = std::move(object.m_shapes);
m_world = object.m_world; m_world = object.m_world;
object.m_handle = nullptr; object.m_handle = nullptr;
return *this; return *this;
} }
void RigidBody2D::Destroy()
{
for (cpShape* shape : m_shapes)
cpShapeFree(shape);
if (m_handle)
cpBodyFree(m_handle);
}
} }