Fixed Memory Leak

As pointed out by @sprado, there was a memory leak in the STL player.
The data pointer was allocated, but never deleted again. Some lines for
deletion were commented out in the memory manager in the free function.
These two lines regarding the deletion of data were moved into the
destructor of the memory manager. The allocation of the data is done in
the allocate() function of the memory manager when a new payload is
generated.
This commit is contained in:
Matthias Jung
2019-08-22 23:11:48 +02:00
parent d55d801a04
commit f6072e9d4f
2 changed files with 18 additions and 16 deletions

View File

@@ -36,6 +36,7 @@
#include "MemoryManager.h"
#include "../common/DebugManager.h"
#include "../controller/core/configuration/Configuration.h"
#include <iostream>
using namespace std;
@@ -48,20 +49,30 @@ MemoryManager::MemoryManager(): numberOfAllocations(0), numberOfFrees(0)
MemoryManager::~MemoryManager()
{
for (gp *payload : freePayloads) {
delete[] payload->get_data_ptr();
delete payload;
numberOfFrees++;
}
//Comment in if you are suspecting a memory leak in the manager
//DebugManager::getInstance().printDebugMessage("MemoryManager","Number of allocated payloads: " + to_string(numberOfAllocations));
//DebugManager::getInstance().printDebugMessage("MemoryManager","Number of freed payloads: " + to_string(numberOfFrees));
DebugManager::getInstance().printDebugMessage("MemoryManager",
"Number of allocated payloads: " + to_string(numberOfAllocations));
DebugManager::getInstance().printDebugMessage("MemoryManager",
"Number of freed payloads: " + to_string(numberOfFrees));
}
gp *MemoryManager::allocate()
{
if (freePayloads.empty()) {
numberOfAllocations++;
return new gp(this);
gp *payload = new gp(this);
// Allocate a data buffer and initialize it with zeroes:
unsigned int dataLength = Configuration::getInstance().getBytesPerBurst();
unsigned char *data = new unsigned char[dataLength];
std::fill(data, data + dataLength, 0);
payload->set_data_ptr(data);
return payload;
} else {
gp *result = freePayloads.back();
freePayloads.pop_back();
@@ -71,8 +82,6 @@ gp *MemoryManager::allocate()
void MemoryManager::free(gp *payload)
{
//unsigned char *dptr = payload->get_data_ptr();
//delete[] dptr;
payload->reset(); //clears all extensions
freePayloads.push_back(payload);
}

View File

@@ -89,12 +89,7 @@ public:
}
// Allocate a generic payload for this request.
gp *payload = this->allocatePayload();
// Allocate a data buffer and initialize it with zeroes. It may be
// overwritten with data from the trace file depending on the storage
// mode.
unsigned char *data = new unsigned char[dataLength];
std::fill(data, data + dataLength, 0);
unsigned char *data = payload->get_data_ptr();
// Trace files MUST provide timestamp, command and address for every
// transaction. The data information depends on the storage mode
@@ -170,16 +165,14 @@ public:
payload->set_data_ptr(data);
payload->set_command(cmd);
if (relative == false)
{
if (relative == false) {
// Send the transaction directly or schedule it to be sent in the future.
if (sendingTime <= sc_time_stamp())
this->payloadEventQueue.notify(*payload, BEGIN_REQ, SC_ZERO_TIME);
else
this->payloadEventQueue.notify(*payload, BEGIN_REQ,
sendingTime - sc_time_stamp());
}
else
} else
payloadEventQueue.notify(*payload, BEGIN_REQ, sendingTime);
}