From d33220f8ca273e83f25942f610f628006253891d Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Fri, 22 Nov 2013 14:23:08 +0100 Subject: [PATCH] Fixed memory leaks Fixed memory leaks. --- .../NetworkDependencies/Connection.cpp | 4 +-- .../Messages/MessageHeader.cpp | 22 +++++++--------- .../Messages/MessageHeader.h | 9 +++---- .../Messages/MessageTest.cpp | 6 ++--- .../Messages/MessageTest.h | 2 +- .../NetworkDependencies.vcxproj | 8 ++++++ .../NetworkDependencies/Translator.cpp | 11 +++++--- Code/Network/NetworkDependencies/Translator.h | 5 ++-- Code/Network/OysterNetworkClient/Client.cpp | 3 ++- .../OysterNetworkClient/ClientMain.cpp | 26 +++++++++---------- .../OysterNetworkClient.vcxproj | 8 ++++++ .../OysterNetworkServer.vcxproj | 8 ++++++ .../OysterNetworkServer/ServerMain.cpp | 13 +++++----- 13 files changed, 73 insertions(+), 52 deletions(-) diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index f13ff159..cac38e71 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -7,9 +7,7 @@ using namespace Oyster::Network; Connection::~Connection() { - mySocket = NULL; - - if(socket != NULL) + if(mySocket != NULL) { closesocket( mySocket ); mySocket = NULL; diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp index cf2d2b22..c2e65faf 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -8,21 +8,23 @@ using namespace Oyster::Network::Protocols; MessageHeader::MessageHeader() { size = 0; - msg = new unsigned char[1024]; + } MessageHeader::~MessageHeader() { - delete[] msg; + } -void MessageHeader::Translate(ProtocolHeader& header) +void MessageHeader::Translate(ProtocolHeader& header, unsigned char msg[] ) { size = 0; - AddInt(header.clientID); - AddInt(header.packageType); - AddInt(header.size); + AddInt(header.clientID, msg); + AddInt(header.packageType, msg); + AddInt(header.size, msg); + + } void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header) @@ -34,18 +36,14 @@ void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header) header.size = GetInt(message); } -unsigned char* MessageHeader::GetMsg() -{ - return msg; -} -void MessageHeader::AddInt(int i) +void MessageHeader::AddInt(int i, unsigned char msg[]) { Pack(&msg[size], i); size += 4; } -void MessageHeader::AddStr(std::string str) +void MessageHeader::AddStr(std::string str, unsigned char msg[]) { Pack(&msg[size], str); size += 2 + str.length(); diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.h b/Code/Network/NetworkDependencies/Messages/MessageHeader.h index 0aa22dff..2705a062 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.h +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.h @@ -20,23 +20,20 @@ namespace Oyster MessageHeader(); virtual ~MessageHeader(); - virtual void Translate(Protocols::ProtocolHeader& header); + virtual void Translate(Protocols::ProtocolHeader& header, unsigned char msg[] ); virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header); - unsigned char* GetMsg(); protected: //Add variables to messages - void AddInt(int i); - void AddStr(std::string str); + void AddInt(int i, unsigned char msg[]); + void AddStr(std::string str, unsigned char msg[]); //Get variables from message int GetInt(unsigned char message[]); std::string GetStr(unsigned char message[]); private: - static const int max_message_length = 1024; - unsigned char* msg; int size; }; diff --git a/Code/Network/NetworkDependencies/Messages/MessageTest.cpp b/Code/Network/NetworkDependencies/Messages/MessageTest.cpp index 89fb2911..e8ddcb57 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageTest.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageTest.cpp @@ -12,11 +12,11 @@ MessageTest::~MessageTest() { } -void MessageTest::Translate(ProtocolHeader& header) +void MessageTest::Translate(ProtocolHeader& header, unsigned char msg[]) { - MessageHeader::Translate(header); + MessageHeader::Translate(header, msg); - AddStr(static_cast(&header)->textMessage); + AddStr(static_cast(&header)->textMessage, msg); } void MessageTest::Translate(unsigned char message[], ProtocolHeader& header) diff --git a/Code/Network/NetworkDependencies/Messages/MessageTest.h b/Code/Network/NetworkDependencies/Messages/MessageTest.h index e01847c9..62a55f24 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageTest.h +++ b/Code/Network/NetworkDependencies/Messages/MessageTest.h @@ -19,7 +19,7 @@ namespace Oyster MessageTest(); virtual ~MessageTest(); - virtual void Translate(Protocols::ProtocolHeader& header); + virtual void Translate(Protocols::ProtocolHeader& header, unsigned char msg[]); virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header); private: diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index 8c215717..c395d509 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -69,21 +69,29 @@ $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)..\External\Lib\$(ProjectName)\ $(ProjectName)_$(PlatformShortName)D + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)..\External\Lib\$(ProjectName)\ $(ProjectName)_$(PlatformShortName) + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) diff --git a/Code/Network/NetworkDependencies/Translator.cpp b/Code/Network/NetworkDependencies/Translator.cpp index 216123cb..ad622fdc 100644 --- a/Code/Network/NetworkDependencies/Translator.cpp +++ b/Code/Network/NetworkDependencies/Translator.cpp @@ -10,7 +10,6 @@ unsigned char* Translator::Translate( ProtocolHeader &header ) switch(header.packageType) { - case package_type_header: message = new MessageHeader(); break; @@ -20,9 +19,15 @@ unsigned char* Translator::Translate( ProtocolHeader &header ) break; } - message->Translate(header); + message->Translate(header, this->msg); - return message->GetMsg(); + if(message != NULL) + { + delete message; + message = NULL; + } + + return msg; } ProtocolSet* Translator::Translate(ProtocolSet* set, unsigned char msg[] ) diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 95002711..26b5a56c 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -12,13 +12,14 @@ namespace Oyster class Translator : public ITranslate { public: - Translator (){ }; - ~Translator(){ }; + Translator () { msg = new unsigned char[256]; }; + ~Translator() { if(msg != NULL) { delete [] this->msg; }}; unsigned char* Translate (Protocols::ProtocolHeader &header ); Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char msg[] ); private: + unsigned char* msg; }; } diff --git a/Code/Network/OysterNetworkClient/Client.cpp b/Code/Network/OysterNetworkClient/Client.cpp index 3569b7d4..d77b1bf7 100644 --- a/Code/Network/OysterNetworkClient/Client.cpp +++ b/Code/Network/OysterNetworkClient/Client.cpp @@ -9,7 +9,8 @@ Client::Client() Client::~Client() { - delete connection; + delete this->connection; + connection = 0; } bool Client::Connect(unsigned int port, char filename[]) diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 359ae7bd..fa812365 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -1,21 +1,19 @@ #include #include "Client.h" #include +#include #include "..\NetworkDependencies\Translator.h" +#include "..\NetworkDependencies\Protocols.h" using namespace std; using namespace Oyster::Network::Protocols;; using namespace Oyster::Network::Client; -#pragma comment(lib, "ws2_32.lib") +#pragma comment(lib, "ws2_32.lib") void ShutdownSockets(); bool InitSockets(); -void chat(Client client); - -#include "../NetworkDependencies/Protocols.h" -#include "../NetworkDependencies/Translator.h" -using namespace Oyster::Network::Protocols; +void chat(Client &client); int main() { @@ -56,11 +54,11 @@ void ShutdownSockets() WSACleanup(); } -void chat(Client client) +void chat(Client &client) { Oyster::Network::Translator *t = new Oyster::Network::Translator(); - unsigned char msgRecv[255] = "\0"; + unsigned char msgRecv[256] = "\0"; string msgSend = ""; ProtocolSet* set = new ProtocolSet; @@ -73,8 +71,8 @@ void chat(Client client) { client.Recv(msgRecv); - set = t->Translate(set, msgRecv); - + t->Translate(set, msgRecv); + switch(set->t) { case package_type_header: @@ -83,17 +81,16 @@ void chat(Client client) cout <<"Client 2: " << set->Protocol.pTest->textMessage <Release(); std::getline(std::cin, msgSend); - + if( msgSend != "exit") { if(msgSend.length() < 1) { - //memcpy(msgSend, "ERROR" , 5); msgSend = "ERROR!"; - //strcpy_s(msgSend, "ERROR"); } test.packageType = package_type_test; @@ -114,5 +111,6 @@ void chat(Client client) } + delete t; delete set; } \ No newline at end of file diff --git a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj index 6bdc7921..4fdfc0d4 100644 --- a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj +++ b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj @@ -69,21 +69,29 @@ $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj index 94a6a6e1..855b323e 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -69,21 +69,29 @@ $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) $(SolutionDir)..\External\Lib\$(ProjectName)\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 2940f87d..6d104944 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "Listener.h" #include "Client.h" using namespace std; @@ -16,7 +17,7 @@ using namespace ::Protocols; int main() { - unsigned char* recvBuffer = new unsigned char[255]; + unsigned char* recvBuffer = new unsigned char[256]; cout << "Server" << endl; Translator t; @@ -42,21 +43,20 @@ int main() client1.Send((unsigned char*)"Hej"); ProtocolSet* set = new ProtocolSet; - + while(1) { client1.Recv(recvBuffer); - - set = t.Translate(set, recvBuffer); + + t.Translate(set, recvBuffer); cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; cout << "Client1: " << set->Protocol.pTest->textMessage << endl; set->Release(); client2.Send(recvBuffer); - client2.Recv(recvBuffer); - set = t.Translate(set, recvBuffer); + t.Translate(set, recvBuffer); cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; cout << "Client2: " << set->Protocol.pTest->textMessage << endl; set->Release(); @@ -64,7 +64,6 @@ int main() } ShutdownSockets(); - delete[] recvBuffer; delete set;