Fix memory leaks in Map and Set classes

This commit is contained in:
Daniel Chappuis 2021-08-13 16:52:17 +02:00
parent e6518f6e87
commit 6db3674c65
4 changed files with 80 additions and 65 deletions

View File

@ -62,6 +62,7 @@ do not hesitate to take a look at the user manual.
- Issue with wrong linear velocity update computed in RigidBody::setLocalCenterOfMass() method - Issue with wrong linear velocity update computed in RigidBody::setLocalCenterOfMass() method
- Issue with wrong linear velocity update computed in RigidBody::updateLocalCenterOfMassFromColliders() method - Issue with wrong linear velocity update computed in RigidBody::updateLocalCenterOfMassFromColliders() method
- Issue with wrong linear velocity update computed in RigidBody::updateMassPropertiesFromColliders() method - Issue with wrong linear velocity update computed in RigidBody::updateMassPropertiesFromColliders() method
- Issue in copy-constructors in Map and Set classes
## Version 0.8.0 (May 31, 2020) ## Version 0.8.0 (May 31, 2020)

View File

@ -239,29 +239,32 @@ class Map {
:mNbAllocatedEntries(map.mNbAllocatedEntries), mNbEntries(map.mNbEntries), mHashSize(map.mHashSize), :mNbAllocatedEntries(map.mNbAllocatedEntries), mNbEntries(map.mNbEntries), mHashSize(map.mHashSize),
mBuckets(nullptr), mEntries(nullptr), mNextEntries(nullptr), mAllocator(map.mAllocator), mFreeIndex(map.mFreeIndex) { mBuckets(nullptr), mEntries(nullptr), mNextEntries(nullptr), mAllocator(map.mAllocator), mFreeIndex(map.mFreeIndex) {
// Allocate memory for the buckets if (mHashSize > 0) {
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
// Allocate memory for the entries // Allocate memory for the buckets
mEntries = static_cast<Pair<K, V>*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(Pair<K, V>))); mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the buckets array // Allocate memory for the entries
std::memcpy(mBuckets, map.mBuckets, mHashSize * sizeof(uint32)); mEntries = static_cast<Pair<K, V>*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(Pair<K, V>)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the next entries indices // Copy the buckets array
std::memcpy(mNextEntries, map.mNextEntries, mNbAllocatedEntries * sizeof(uint32)); std::memcpy(mBuckets, map.mBuckets, mHashSize * sizeof(uint32));
// Copy the entries // Copy the next entries indices
for (uint32 i=0; i<mHashSize; i++) { std::memcpy(mNextEntries, map.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
uint32 entryIndex = mBuckets[i]; // Copy the entries
while(entryIndex != INVALID_INDEX) { for (uint32 i=0; i<mHashSize; i++) {
// Copy the entry to the new location and destroy the previous one uint32 entryIndex = mBuckets[i];
new (mEntries + entryIndex) Pair<K,V>(map.mEntries[entryIndex]); while(entryIndex != INVALID_INDEX) {
entryIndex = mNextEntries[entryIndex]; // Copy the entry to the new location and destroy the previous one
new (mEntries + entryIndex) Pair<K,V>(map.mEntries[entryIndex]);
entryIndex = mNextEntries[entryIndex];
}
} }
} }
} }
@ -640,29 +643,32 @@ class Map {
mHashSize = map.mHashSize; mHashSize = map.mHashSize;
mFreeIndex = map.mFreeIndex; mFreeIndex = map.mFreeIndex;
// Allocate memory for the buckets if (mHashSize > 0) {
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
// Allocate memory for the entries // Allocate memory for the buckets
mEntries = static_cast<Pair<K,V>*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(Pair<K,V>))); mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the buckets array // Allocate memory for the entries
std::memcpy(mBuckets, map.mBuckets, mHashSize * sizeof(uint32)); mEntries = static_cast<Pair<K,V>*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(Pair<K,V>)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the next entries indices // Copy the buckets array
std::memcpy(mNextEntries, map.mNextEntries, mNbAllocatedEntries * sizeof(uint32)); std::memcpy(mBuckets, map.mBuckets, mHashSize * sizeof(uint32));
// Copy the entries // Copy the next entries indices
for (uint32 i=0; i<mHashSize; i++) { std::memcpy(mNextEntries, map.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
uint32 entryIndex = mBuckets[i]; // Copy the entries
while(entryIndex != INVALID_INDEX) { for (uint32 i=0; i<mHashSize; i++) {
// Copy the entry to the new location and destroy the previous one uint32 entryIndex = mBuckets[i];
new (mEntries + entryIndex) Pair<K,V>(map.mEntries[entryIndex]); while(entryIndex != INVALID_INDEX) {
entryIndex = mNextEntries[entryIndex]; // Copy the entry to the new location and destroy the previous one
new (mEntries + entryIndex) Pair<K,V>(map.mEntries[entryIndex]);
entryIndex = mNextEntries[entryIndex];
}
} }
} }
} }

View File

@ -239,29 +239,32 @@ class Set {
:mNbAllocatedEntries(set.mNbAllocatedEntries), mNbEntries(set.mNbEntries), mHashSize(set.mHashSize), :mNbAllocatedEntries(set.mNbAllocatedEntries), mNbEntries(set.mNbEntries), mHashSize(set.mHashSize),
mBuckets(nullptr), mEntries(nullptr), mNextEntries(nullptr), mAllocator(set.mAllocator), mFreeIndex(set.mFreeIndex) { mBuckets(nullptr), mEntries(nullptr), mNextEntries(nullptr), mAllocator(set.mAllocator), mFreeIndex(set.mFreeIndex) {
// Allocate memory for the buckets if (mHashSize > 0) {
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
// Allocate memory for the entries // Allocate memory for the buckets
mEntries = static_cast<V*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(V))); mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the buckets array // Allocate memory for the entries
std::memcpy(mBuckets, set.mBuckets, mHashSize * sizeof(uint32)); mEntries = static_cast<V*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(V)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the next entries indices // Copy the buckets array
std::memcpy(mNextEntries, set.mNextEntries, mNbAllocatedEntries * sizeof(uint32)); std::memcpy(mBuckets, set.mBuckets, mHashSize * sizeof(uint32));
// Copy the entries // Copy the next entries indices
for (uint32 i=0; i<mHashSize; i++) { std::memcpy(mNextEntries, set.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
uint32 entryIndex = mBuckets[i]; // Copy the entries
while(entryIndex != INVALID_INDEX) { for (uint32 i=0; i<mHashSize; i++) {
// Copy the entry to the new location and destroy the previous one uint32 entryIndex = mBuckets[i];
new (mEntries + entryIndex) V(set.mEntries[entryIndex]); while(entryIndex != INVALID_INDEX) {
entryIndex = mNextEntries[entryIndex]; // Copy the entry to the new location and destroy the previous one
new (mEntries + entryIndex) V(set.mEntries[entryIndex]);
entryIndex = mNextEntries[entryIndex];
}
} }
} }
} }
@ -604,29 +607,32 @@ class Set {
mHashSize = set.mHashSize; mHashSize = set.mHashSize;
mFreeIndex = set.mFreeIndex; mFreeIndex = set.mFreeIndex;
// Allocate memory for the buckets if (mHashSize > 0) {
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
// Allocate memory for the entries // Allocate memory for the buckets
mEntries = static_cast<V*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(V))); mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the buckets array // Allocate memory for the entries
std::memcpy(mBuckets, set.mBuckets, mHashSize * sizeof(uint32)); mEntries = static_cast<V*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(V)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the next entries indices // Copy the buckets array
std::memcpy(mNextEntries, set.mNextEntries, mNbAllocatedEntries * sizeof(uint32)); std::memcpy(mBuckets, set.mBuckets, mHashSize * sizeof(uint32));
// Copy the entries // Copy the next entries indices
for (uint32 i=0; i<mHashSize; i++) { std::memcpy(mNextEntries, set.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
uint32 entryIndex = mBuckets[i]; // Copy the entries
while(entryIndex != INVALID_INDEX) { for (uint32 i=0; i<mHashSize; i++) {
// Copy the entry to the new location and destroy the previous one uint32 entryIndex = mBuckets[i];
new (mEntries + entryIndex) V(set.mEntries[entryIndex]); while(entryIndex != INVALID_INDEX) {
entryIndex = mNextEntries[entryIndex]; // Copy the entry to the new location and destroy the previous one
new (mEntries + entryIndex) V(set.mEntries[entryIndex]);
entryIndex = mNextEntries[entryIndex];
}
} }
} }
} }

View File

@ -58,7 +58,6 @@ class TestRigidBody : public Test {
PolygonVertexArray* mConvexMeshPolygonVertexArray; PolygonVertexArray* mConvexMeshPolygonVertexArray;
PolyhedronMesh* mConvexMeshPolyhedronMesh; PolyhedronMesh* mConvexMeshPolyhedronMesh;
PolygonVertexArray::PolygonFace* mConvexMeshPolygonFaces; PolygonVertexArray::PolygonFace* mConvexMeshPolygonFaces;
TriangleVertexArray* mConcaveMeshTriangleVertexArray;
float mConvexMeshCubeVertices[8 * 3]; float mConvexMeshCubeVertices[8 * 3];
int mConvexMeshCubeIndices[24]; int mConvexMeshCubeIndices[24];
@ -137,6 +136,9 @@ class TestRigidBody : public Test {
mWorld->destroyRigidBody(mRigidBody1); mWorld->destroyRigidBody(mRigidBody1);
mWorld->destroyRigidBody(mRigidBody2Box); mWorld->destroyRigidBody(mRigidBody2Box);
mWorld->destroyRigidBody(mRigidBody2Sphere); mWorld->destroyRigidBody(mRigidBody2Sphere);
delete[] mConvexMeshPolygonFaces;
delete mConvexMeshPolygonVertexArray;
} }
/// Run the tests /// Run the tests