Fixed memory leaks

Fixed memory leaks.
This commit is contained in:
Pontus Fransson 2013-11-22 14:23:08 +01:00
parent 623cffced6
commit d33220f8ca
13 changed files with 73 additions and 52 deletions

View File

@ -7,9 +7,7 @@ using namespace Oyster::Network;
Connection::~Connection() Connection::~Connection()
{ {
mySocket = NULL; if(mySocket != NULL)
if(socket != NULL)
{ {
closesocket( mySocket ); closesocket( mySocket );
mySocket = NULL; mySocket = NULL;

View File

@ -8,21 +8,23 @@ using namespace Oyster::Network::Protocols;
MessageHeader::MessageHeader() MessageHeader::MessageHeader()
{ {
size = 0; size = 0;
msg = new unsigned char[1024];
} }
MessageHeader::~MessageHeader() MessageHeader::~MessageHeader()
{ {
delete[] msg;
} }
void MessageHeader::Translate(ProtocolHeader& header) void MessageHeader::Translate(ProtocolHeader& header, unsigned char msg[] )
{ {
size = 0; size = 0;
AddInt(header.clientID); AddInt(header.clientID, msg);
AddInt(header.packageType); AddInt(header.packageType, msg);
AddInt(header.size); AddInt(header.size, msg);
} }
void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header) void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header)
@ -34,18 +36,14 @@ void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header)
header.size = GetInt(message); 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); Pack(&msg[size], i);
size += 4; size += 4;
} }
void MessageHeader::AddStr(std::string str) void MessageHeader::AddStr(std::string str, unsigned char msg[])
{ {
Pack(&msg[size], str); Pack(&msg[size], str);
size += 2 + str.length(); size += 2 + str.length();

View File

@ -20,23 +20,20 @@ namespace Oyster
MessageHeader(); MessageHeader();
virtual ~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); virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header);
unsigned char* GetMsg();
protected: protected:
//Add variables to messages //Add variables to messages
void AddInt(int i); void AddInt(int i, unsigned char msg[]);
void AddStr(std::string str); void AddStr(std::string str, unsigned char msg[]);
//Get variables from message //Get variables from message
int GetInt(unsigned char message[]); int GetInt(unsigned char message[]);
std::string GetStr(unsigned char message[]); std::string GetStr(unsigned char message[]);
private: private:
static const int max_message_length = 1024;
unsigned char* msg;
int size; int size;
}; };

View File

@ -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<ProtocolTest*>(&header)->textMessage); AddStr(static_cast<ProtocolTest*>(&header)->textMessage, msg);
} }
void MessageTest::Translate(unsigned char message[], ProtocolHeader& header) void MessageTest::Translate(unsigned char message[], ProtocolHeader& header)

View File

@ -19,7 +19,7 @@ namespace Oyster
MessageTest(); MessageTest();
virtual ~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); virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header);
private: private:

View File

@ -69,21 +69,29 @@
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>

View File

@ -10,7 +10,6 @@ unsigned char* Translator::Translate( ProtocolHeader &header )
switch(header.packageType) switch(header.packageType)
{ {
case package_type_header: case package_type_header:
message = new MessageHeader(); message = new MessageHeader();
break; break;
@ -20,9 +19,15 @@ unsigned char* Translator::Translate( ProtocolHeader &header )
break; 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[] ) ProtocolSet* Translator::Translate(ProtocolSet* set, unsigned char msg[] )

View File

@ -12,13 +12,14 @@ namespace Oyster
class Translator : public ITranslate class Translator : public ITranslate
{ {
public: public:
Translator (){ }; Translator () { msg = new unsigned char[256]; };
~Translator(){ }; ~Translator() { if(msg != NULL) { delete [] this->msg; }};
unsigned char* Translate (Protocols::ProtocolHeader &header ); unsigned char* Translate (Protocols::ProtocolHeader &header );
Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char msg[] ); Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char msg[] );
private: private:
unsigned char* msg;
}; };
} }

View File

@ -9,7 +9,8 @@ Client::Client()
Client::~Client() Client::~Client()
{ {
delete connection; delete this->connection;
connection = 0;
} }
bool Client::Connect(unsigned int port, char filename[]) bool Client::Connect(unsigned int port, char filename[])

View File

@ -1,21 +1,19 @@
#include <iostream> #include <iostream>
#include "Client.h" #include "Client.h"
#include <WinSock2.h> #include <WinSock2.h>
#include <vld.h>
#include "..\NetworkDependencies\Translator.h" #include "..\NetworkDependencies\Translator.h"
#include "..\NetworkDependencies\Protocols.h"
using namespace std; using namespace std;
using namespace Oyster::Network::Protocols;; using namespace Oyster::Network::Protocols;;
using namespace Oyster::Network::Client; using namespace Oyster::Network::Client;
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "ws2_32.lib")
void ShutdownSockets(); void ShutdownSockets();
bool InitSockets(); bool InitSockets();
void chat(Client client); void chat(Client &client);
#include "../NetworkDependencies/Protocols.h"
#include "../NetworkDependencies/Translator.h"
using namespace Oyster::Network::Protocols;
int main() int main()
{ {
@ -56,11 +54,11 @@ void ShutdownSockets()
WSACleanup(); WSACleanup();
} }
void chat(Client client) void chat(Client &client)
{ {
Oyster::Network::Translator *t = new Oyster::Network::Translator(); Oyster::Network::Translator *t = new Oyster::Network::Translator();
unsigned char msgRecv[255] = "\0"; unsigned char msgRecv[256] = "\0";
string msgSend = ""; string msgSend = "";
ProtocolSet* set = new ProtocolSet; ProtocolSet* set = new ProtocolSet;
@ -73,7 +71,7 @@ void chat(Client client)
{ {
client.Recv(msgRecv); client.Recv(msgRecv);
set = t->Translate(set, msgRecv); t->Translate(set, msgRecv);
switch(set->t) switch(set->t)
{ {
@ -84,6 +82,7 @@ void chat(Client client)
break; break;
} }
set->Release();
std::getline(std::cin, msgSend); std::getline(std::cin, msgSend);
@ -91,9 +90,7 @@ void chat(Client client)
{ {
if(msgSend.length() < 1) if(msgSend.length() < 1)
{ {
//memcpy(msgSend, "ERROR" , 5);
msgSend = "ERROR!"; msgSend = "ERROR!";
//strcpy_s(msgSend, "ERROR");
} }
test.packageType = package_type_test; test.packageType = package_type_test;
@ -114,5 +111,6 @@ void chat(Client client)
} }
delete t;
delete set; delete set;
} }

View File

@ -69,21 +69,29 @@
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>

View File

@ -69,21 +69,29 @@
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)D</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir> <OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir> <IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<TargetName>$(ProjectName)_$(PlatformShortName)</TargetName> <TargetName>$(ProjectName)_$(PlatformShortName)</TargetName>
<IncludePath>C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath)</IncludePath>
<LibraryPath>C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath)</LibraryPath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>

View File

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <WinSock2.h> #include <WinSock2.h>
#include <vld.h>
#include "Listener.h" #include "Listener.h"
#include "Client.h" #include "Client.h"
using namespace std; using namespace std;
@ -16,7 +17,7 @@ using namespace ::Protocols;
int main() int main()
{ {
unsigned char* recvBuffer = new unsigned char[255]; unsigned char* recvBuffer = new unsigned char[256];
cout << "Server" << endl; cout << "Server" << endl;
Translator t; Translator t;
@ -47,16 +48,15 @@ int main()
{ {
client1.Recv(recvBuffer); 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 << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
cout << "Client1: " << set->Protocol.pTest->textMessage << endl; cout << "Client1: " << set->Protocol.pTest->textMessage << endl;
set->Release(); set->Release();
client2.Send(recvBuffer); client2.Send(recvBuffer);
client2.Recv(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 << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl;
cout << "Client2: " << set->Protocol.pTest->textMessage << endl; cout << "Client2: " << set->Protocol.pTest->textMessage << endl;
set->Release(); set->Release();
@ -64,7 +64,6 @@ int main()
} }
ShutdownSockets(); ShutdownSockets();
delete[] recvBuffer; delete[] recvBuffer;
delete set; delete set;