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::updateLocalCenterOfMassFromColliders() 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)

View File

@ -239,29 +239,32 @@ class Map {
:mNbAllocatedEntries(map.mNbAllocatedEntries), mNbEntries(map.mNbEntries), mHashSize(map.mHashSize),
mBuckets(nullptr), mEntries(nullptr), mNextEntries(nullptr), mAllocator(map.mAllocator), mFreeIndex(map.mFreeIndex) {
// Allocate memory for the buckets
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
if (mHashSize > 0) {
// Allocate memory for the entries
mEntries = static_cast<Pair<K, V>*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(Pair<K, V>)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Allocate memory for the buckets
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
// Copy the buckets array
std::memcpy(mBuckets, map.mBuckets, mHashSize * sizeof(uint32));
// Allocate memory for the entries
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
std::memcpy(mNextEntries, map.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
// Copy the buckets array
std::memcpy(mBuckets, map.mBuckets, mHashSize * sizeof(uint32));
// Copy the entries
for (uint32 i=0; i<mHashSize; i++) {
// Copy the next entries indices
std::memcpy(mNextEntries, map.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
uint32 entryIndex = mBuckets[i];
while(entryIndex != INVALID_INDEX) {
// Copy the entries
for (uint32 i=0; i<mHashSize; i++) {
// Copy the entry to the new location and destroy the previous one
new (mEntries + entryIndex) Pair<K,V>(map.mEntries[entryIndex]);
uint32 entryIndex = mBuckets[i];
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;
mFreeIndex = map.mFreeIndex;
// Allocate memory for the buckets
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
if (mHashSize > 0) {
// Allocate memory for the entries
mEntries = static_cast<Pair<K,V>*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(Pair<K,V>)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Allocate memory for the buckets
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
// Copy the buckets array
std::memcpy(mBuckets, map.mBuckets, mHashSize * sizeof(uint32));
// Allocate memory for the entries
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
std::memcpy(mNextEntries, map.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
// Copy the buckets array
std::memcpy(mBuckets, map.mBuckets, mHashSize * sizeof(uint32));
// Copy the entries
for (uint32 i=0; i<mHashSize; i++) {
// Copy the next entries indices
std::memcpy(mNextEntries, map.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
uint32 entryIndex = mBuckets[i];
while(entryIndex != INVALID_INDEX) {
// Copy the entries
for (uint32 i=0; i<mHashSize; i++) {
// Copy the entry to the new location and destroy the previous one
new (mEntries + entryIndex) Pair<K,V>(map.mEntries[entryIndex]);
uint32 entryIndex = mBuckets[i];
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),
mBuckets(nullptr), mEntries(nullptr), mNextEntries(nullptr), mAllocator(set.mAllocator), mFreeIndex(set.mFreeIndex) {
// Allocate memory for the buckets
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
if (mHashSize > 0) {
// Allocate memory for the entries
mEntries = static_cast<V*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(V)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Allocate memory for the buckets
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
// Copy the buckets array
std::memcpy(mBuckets, set.mBuckets, mHashSize * sizeof(uint32));
// Allocate memory for the entries
mEntries = static_cast<V*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(V)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the next entries indices
std::memcpy(mNextEntries, set.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
// Copy the buckets array
std::memcpy(mBuckets, set.mBuckets, mHashSize * sizeof(uint32));
// Copy the entries
for (uint32 i=0; i<mHashSize; i++) {
// Copy the next entries indices
std::memcpy(mNextEntries, set.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
uint32 entryIndex = mBuckets[i];
while(entryIndex != INVALID_INDEX) {
// Copy the entries
for (uint32 i=0; i<mHashSize; i++) {
// Copy the entry to the new location and destroy the previous one
new (mEntries + entryIndex) V(set.mEntries[entryIndex]);
uint32 entryIndex = mBuckets[i];
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;
mFreeIndex = set.mFreeIndex;
// Allocate memory for the buckets
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
if (mHashSize > 0) {
// Allocate memory for the entries
mEntries = static_cast<V*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(V)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Allocate memory for the buckets
mBuckets = static_cast<uint32*>(mAllocator.allocate(mHashSize * sizeof(uint32)));
// Copy the buckets array
std::memcpy(mBuckets, set.mBuckets, mHashSize * sizeof(uint32));
// Allocate memory for the entries
mEntries = static_cast<V*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(V)));
mNextEntries = static_cast<uint32*>(mAllocator.allocate(mNbAllocatedEntries * sizeof(uint32)));
// Copy the next entries indices
std::memcpy(mNextEntries, set.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
// Copy the buckets array
std::memcpy(mBuckets, set.mBuckets, mHashSize * sizeof(uint32));
// Copy the entries
for (uint32 i=0; i<mHashSize; i++) {
// Copy the next entries indices
std::memcpy(mNextEntries, set.mNextEntries, mNbAllocatedEntries * sizeof(uint32));
uint32 entryIndex = mBuckets[i];
while(entryIndex != INVALID_INDEX) {
// Copy the entries
for (uint32 i=0; i<mHashSize; i++) {
// Copy the entry to the new location and destroy the previous one
new (mEntries + entryIndex) V(set.mEntries[entryIndex]);
uint32 entryIndex = mBuckets[i];
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;
PolyhedronMesh* mConvexMeshPolyhedronMesh;
PolygonVertexArray::PolygonFace* mConvexMeshPolygonFaces;
TriangleVertexArray* mConcaveMeshTriangleVertexArray;
float mConvexMeshCubeVertices[8 * 3];
int mConvexMeshCubeIndices[24];
@ -137,6 +136,9 @@ class TestRigidBody : public Test {
mWorld->destroyRigidBody(mRigidBody1);
mWorld->destroyRigidBody(mRigidBody2Box);
mWorld->destroyRigidBody(mRigidBody2Sphere);
delete[] mConvexMeshPolygonFaces;
delete mConvexMeshPolygonVertexArray;
}
/// Run the tests