Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CCCS-19: Fix Memory Leaks #20

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Src/Emitter/Bin/emitter",
"args": ["50"],
"args": ["500"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/Src/Emitter/Bin",
"environment": [],
Expand Down
16 changes: 10 additions & 6 deletions Src/Collector/CollectorService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ bool CollectorService::ReadIncomingMessage(int socketId, bool &stop_signal)
memset(buffer, '\0', sz);
server->ReadBinaryContent(socketId, buffer, sz);

Measure meaIn = *(Measure::FromBinary(buffer));
Measure *meaIn = Measure::FromBinary(buffer);

LOG(DEBUG) << "Readen from socketId " << socketId << ": " << meaIn.To2String();
LOG(DEBUG) << "Readen from socketId " << socketId << ": " << meaIn->To2String();

NotifySubscribers(&meaIn);
NotifySubscribers(meaIn);

delete (meaIn);
}
break;

Expand All @@ -131,11 +133,13 @@ bool CollectorService::ReadIncomingMessage(int socketId, bool &stop_signal)
memset(buffer, '\0', sz);
server->ReadBinaryContent(socketId, buffer, sz);

CommTerm meaIn = *(CommTerm::FromBinary(buffer));
CommTerm *termIn = CommTerm::FromBinary(buffer);

LOG(DEBUG) << "Readen from socketId " << socketId << ": " << termIn->To2String();

LOG(DEBUG) << "Readen from socketId " << socketId << ": " << meaIn.To2String();
NotifySubscribers(termIn);

NotifySubscribers(&meaIn);
delete (termIn);

stop_signal = true;
}
Expand Down
17 changes: 10 additions & 7 deletions Src/Emitter/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <thread> // std::this_thread

#include "../Library/MemoryMonitor/MemoryMonitor.hpp"
#include "../Library/ELog/easylogging++.h"
Expand Down Expand Up @@ -78,28 +79,30 @@ int main(int argc, char **argv)
for (int i = 1; i <= msg_count; i++)
{
Measure *msg = new Measure(20040501, 23.1 + i / 10, "Measure");
unsigned char *p = nullptr;
int sz = 0;

int sz = 0;
unsigned char *p = nullptr;
BinaryWrapper::WrapMessage(msg, p, sz);

client.Send(p, sz);
free(p);

LOG(DEBUG) << "Sent: " << msg->To2String();
LOG(INFO) << "Sent: " << msg->To2String();

sleep(1);
delete (msg);

std::this_thread::sleep_for(std::chrono::milliseconds(50));
}

// Send communication closing signal
{
CommTerm *msg = new CommTerm();
unsigned char *p = nullptr;

int sz = 0;

unsigned char *p = nullptr;
BinaryWrapper::WrapMessage(msg, p, sz);

client.Send(p, sz);
free(p);

LOG(DEBUG) << "Sent: " << msg->To2String();
LOG(INFO) << "Sent: " << msg->To2String();
Expand Down
2 changes: 2 additions & 0 deletions Src/Library/BinaryWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ void BinaryWrapper::WrapMessage(IMessage *input, unsigned char *&p, int &sz)

p = outer_data_content;
sz = outer_data_size;

free(inner_data_content);
}
6 changes: 4 additions & 2 deletions Src/Library/CommTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ std::string CommTerm::To2String()
snprintf(str, 80, "Disconnect %02d:%02d:%02d\n",
t->tm_hour, t->tm_min, t->tm_sec);

return std::string(str);
}
std::string result = std::string(str);
free(str);

return result;
}

void CommTerm::ToBinary(unsigned char *&p, size_t &sz)
{
Expand Down
2 changes: 1 addition & 1 deletion Src/Library/CommTerm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CommTerm : public IMessage
std::time_t time;

public:
CommTerm(/* args */);
CommTerm();
~CommTerm();

std::string To2String();
Expand Down
7 changes: 5 additions & 2 deletions Src/Library/IMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ class IMessage
char *str = (char *)calloc(80, 1);
snprintf(str, 80, "Message type: %d\n", type_sign);

return std::string(str);
std::string result = std::string(str);
free(str);

return result;
}

virtual void ToBinary(unsigned char *&p, size_t &sz) = 0;

IMessage(unsigned char t) { type_sign = t; }
Expand Down
5 changes: 4 additions & 1 deletion Src/Library/Measure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ std::string Measure::To2String()
t->tm_hour, t->tm_min, t->tm_sec,
temperature, circumstances.c_str());

return std::string(str);
std::string result = std::string(str);
free(str);

return result;
}

void Measure::ToBinary(unsigned char *&p, size_t &sz)
Expand Down
13 changes: 6 additions & 7 deletions Src/Library/MemoryMonitor/MemoryStatus.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
#include <fstream> // std::ifstream
#include <fstream> // std::ifstream

#include "MemoryStatus.hpp"

std::string MemoryStatus::To2String()
{
tm *t = std::localtime(&time);
char *str = (char *)calloc(80, 1);
snprintf(str, 80, "%02d:%02d:%02d VmSize=%u VmData=%u VmStk=%u VmSwap=%u",
t->tm_hour, t->tm_min, t->tm_sec,
VmSize, VmData, VmStk, VmSwap);
snprintf(str, 80, "%02d:%02d:%02d VmSize=%u VmData=%u VmSwap=%u",
t->tm_hour, t->tm_min, t->tm_sec, VmSize, VmData, VmSwap);

std::string result=std::string(str);
std::string result = std::string(str);

return result;
}

MemoryStatus::MemoryStatus(std::string path)
{
time = std::time(nullptr);
time = std::time(nullptr);

std::ifstream infile (path, std::ifstream::in);
std::ifstream infile(path, std::ifstream::in);
std::string line;

while (std::getline(infile, line))
Expand Down
4 changes: 2 additions & 2 deletions Src/Meter/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ int main(int argc, char **argv)

if (process_found && ms.VmSize == 0)
{
LOG(DEBUG) << "Process exited; exit Monitor";
LOG(INFO) << "Process exited; exit Monitor";
LOG(DEBUG) << "Process \"" << process_name << "\" exited; exit Meter";
LOG(INFO) << "Process \"" << process_name << "\" exited; exit Meter";
break;
}

Expand Down