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()
{
mySocket = NULL;
if(socket != NULL)
if(mySocket != NULL)
{
closesocket( mySocket );
mySocket = NULL;

View File

@ -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();

View File

@ -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;
};

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)

View File

@ -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:

View File

@ -69,21 +69,29 @@
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<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>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>

View File

@ -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[] )

View File

@ -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;
};
}

View File

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

View File

@ -1,21 +1,19 @@
#include <iostream>
#include "Client.h"
#include <WinSock2.h>
#include <vld.h>
#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,7 +71,7 @@ void chat(Client client)
{
client.Recv(msgRecv);
set = t->Translate(set, msgRecv);
t->Translate(set, msgRecv);
switch(set->t)
{
@ -84,6 +82,7 @@ void chat(Client client)
break;
}
set->Release();
std::getline(std::cin, msgSend);
@ -91,9 +90,7 @@ void chat(Client client)
{
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;
}

View File

@ -69,21 +69,29 @@
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>

View File

@ -69,21 +69,29 @@
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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 Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)..\External\Lib\$(ProjectName)\</OutDir>
<IntDir>$(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\</IntDir>
<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>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <WinSock2.h>
#include <vld.h>
#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;
@ -47,16 +48,15 @@ int main()
{
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;