Skip to content

Commit

Permalink
Fixed a couple of memory leaks.
Browse files Browse the repository at this point in the history
Replaced some char array allocations with strings.
  • Loading branch information
ccw808 committed Apr 28, 2010
1 parent 66207e1 commit 8b51d10
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 104 deletions.
6 changes: 2 additions & 4 deletions MTA10/mods/deathmatch/logic/CServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ char* szServerErrors[4] =
CServer::CServer ( void )
{
assert ( !g_bIsStarted );
m_szConfig = NULL;

// Initialize
m_bIsReady = false;
Expand Down Expand Up @@ -133,8 +132,7 @@ bool CServer::Start ( const char* szConfig )
// Not already started?
if ( !g_bIsStarted )
{
m_szConfig = new char [ strlen ( szConfig ) + 1 ];
strcpy ( m_szConfig, szConfig );
m_strConfig = szConfig;

// Check that the DLL exists
if ( !DoesFileExist ( m_strDLLFile ) )
Expand Down Expand Up @@ -302,7 +300,7 @@ unsigned long CServer::Thread_Run ( void )
strcpy ( szArgument3, "--config" );

char szArgument4 [64];
strcpy ( szArgument4, m_szConfig );
strcpy ( szArgument4, m_strConfig );

char szArgument5 [8];
strcpy ( szArgument5, "-s" );
Expand Down
2 changes: 1 addition & 1 deletion MTA10/mods/deathmatch/logic/CServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CServer
CCriticalSection m_CriticalSection;
char m_szServerRoot [MAX_PATH];
SString m_strDLLFile;
char* m_szConfig;
SString m_strConfig;

int m_iLastError;

Expand Down
2 changes: 2 additions & 0 deletions MTA10/mods/deathmatch/logic/rpc/CInputRPCs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,14 @@ void CInputRPCs::ForceReconnect ( NetBitStreamInterface& bitStream )
if ( szPassword )
{
g_pCore->Reconnect ( szHost, usPort, szPassword );
delete [] szHost;
return;
}
}

g_pCore->Reconnect ( szHost, usPort, NULL );
}
delete [] szHost;
}
}

Expand Down
2 changes: 1 addition & 1 deletion MTA10_Server/mods/deathmatch/logic/CPlayerTextManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void CPlayerTextManager::Process ()
// Tell the player
m_pPlayer->Send ( CServerTextItemPacket( textItem->m_ulUniqueId, textItem->m_bDeletable, textItem->m_vecPosition.fX,
textItem->m_vecPosition.fY, textItem->m_fScale,
textItem->m_Color, textItem->m_ucFormat, textItem->m_ucShadowAlpha, textItem->m_szText ) );
textItem->m_Color, textItem->m_ucFormat, textItem->m_ucShadowAlpha, textItem->m_strText ) );

// Delete the text item created on the queue
delete textItem;
Expand Down
2 changes: 1 addition & 1 deletion MTA10_Server/mods/deathmatch/logic/CRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct CRegistryResultCell
~CRegistryResultCell ( void )
{
if ( pVal )
delete pVal;
delete [] pVal;
}

int nType; // Type identifier, SQLITE_*
Expand Down
75 changes: 11 additions & 64 deletions MTA10_Server/mods/deathmatch/logic/CTextItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,15 @@

#include "StdInc.h"

unsigned long ulUniqueId = 0;
static unsigned long ulUniqueId = 0;

CTextItem::CTextItem ( const char* szText, const CVector2D& vecPosition, eTextPriority Priority, const SColor color, float fScale, unsigned char ucFormat, unsigned char ucShadowAlpha )
{
// Assign us an unique ID
m_ulUniqueId = ulUniqueId++;

// Copy the text
if ( szText )
{
m_szText = new char [ strlen ( szText ) + 1];
strcpy ( m_szText, szText );
}
else
{
m_szText = new char [1];
m_szText [0] = 0;
}
m_strText = szText ? szText : "";

// Assign the properties
m_vecPosition = vecPosition;
Expand All @@ -45,16 +36,7 @@ CTextItem::CTextItem ( const char* szText, const CVector2D& vecPosition, eTextPr
CTextItem::CTextItem ( const CTextItem& TextItem )
{
// Copy the tex
if ( TextItem.m_szText )
{
m_szText = new char [strlen ( TextItem.m_szText ) + 1];
strcpy ( m_szText, TextItem.m_szText );
}
else
{
m_szText = new char [1];
m_szText [0] = 0;
}
m_strText = TextItem.m_strText;

// Copy over the properties
m_ulUniqueId = TextItem.m_ulUniqueId;
Expand All @@ -70,19 +52,8 @@ CTextItem::CTextItem ( const CTextItem& TextItem )

bool CTextItem::operator= ( const CTextItem& TextItem )
{
// Delete the previous title buffer and copy the buffer in the text item given
if ( m_szText ) delete [] m_szText;

if ( TextItem.m_szText )
{
m_szText = new char [strlen ( TextItem.m_szText ) + 1];
strcpy ( m_szText, TextItem.m_szText );
}
else
{
m_szText = new char [1];
m_szText [0] = 0;
}
// Copy the new text
m_strText = TextItem.m_strText;

// Copy the properties
m_ulUniqueId = TextItem.m_ulUniqueId;
Expand All @@ -104,13 +75,6 @@ CTextItem::~CTextItem ( )
// Tell all our observers about it
m_bDeletable = true;
NotifyObservers ();

// Delete our text
if ( m_szText )
{
delete [] m_szText;
m_szText = NULL;
}
}


Expand All @@ -137,31 +101,15 @@ void CTextItem::NotifyObservers ( void )

void CTextItem::SetText ( const char* szText )
{
// If the text is the same, don't bother
if ( m_szText && strcmp ( m_szText, szText ) == 0 )
// If the text has changed
if ( m_strText != szText )
{
return;
}
// Update
m_strText = szText;

// Delete the previous buffer if neccessary
if ( m_szText )
{
delete [] m_szText;
// Tell the text displays
NotifyObservers ();
}

// Allocate and copy the new text
m_szText = new char [strlen ( szText ) + 1];
strcpy ( m_szText, szText );

// Tell the text displays
NotifyObservers ();
}


char* CTextItem::GetText ( char* pBuffer, size_t bufferSize )
{
strncpy ( pBuffer, m_szText, bufferSize );
return pBuffer;
}


Expand Down Expand Up @@ -193,7 +141,6 @@ void CTextItem::SetColor ( const SColor color )
}



void CTextItem::SetScale ( float fScale )
{
// If the scale has changed
Expand Down
4 changes: 2 additions & 2 deletions MTA10_Server/mods/deathmatch/logic/CTextItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CTextItem
bool operator= ( const CTextItem& TextItem );

void SetText ( const char* szText );
char* GetText ( char* pBuffer, size_t bufferSize );
const SString& GetText ( void ) { return m_strText; };

inline const CVector2D& GetPosition ( void ) { return m_vecPosition; };
void SetPosition ( const CVector2D& vecPosition );
Expand All @@ -60,7 +60,7 @@ class CTextItem
inline bool IsBeingDeleted ( void ) { return m_bDeletable; };

private:
char * m_szText;
SString m_strText;
CVector2D m_vecPosition;
SColor m_Color;
float m_fScale;
Expand Down
4 changes: 1 addition & 3 deletions MTA10_Server/mods/deathmatch/logic/luadefs/CLuaTextDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,7 @@ int CLuaTextDefs::textItemGetText ( lua_State* luaVM )
CTextItem* pTextItem = lua_totextitem ( luaVM, 1 );
if ( pTextItem )
{
char szBuffer[1024];
pTextItem->GetText ( szBuffer, 1024 );
lua_pushstring ( luaVM, szBuffer );
lua_pushstring ( luaVM, pTextItem->GetText() );
return 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@

#include "StdInc.h"

CServerTextItemPacket::CServerTextItemPacket( void )
{
m_szText = NULL;
}


CServerTextItemPacket::CServerTextItemPacket( unsigned long ulUniqueId, bool bDeleteable, float fX, float fY, float fScale, const SColor color, unsigned char format, unsigned char ucShadowAlpha, char* szText )
CServerTextItemPacket::CServerTextItemPacket( unsigned long ulUniqueId, bool bDeleteable, float fX, float fY, float fScale, const SColor color, unsigned char format, unsigned char ucShadowAlpha, const char* szText )
{
m_ulUniqueId = ulUniqueId;
m_bDeletable = bDeleteable;
Expand All @@ -29,17 +23,7 @@ CServerTextItemPacket::CServerTextItemPacket( unsigned long ulUniqueId, bool bDe
m_Color = color;
m_ucFormat = format;
m_ucShadowAlpha = ucShadowAlpha;
m_szText = new char [strlen ( szText ) + 1];
strcpy ( m_szText, szText );
}


CServerTextItemPacket::~CServerTextItemPacket ( void )
{
if ( m_szText )
{
delete [] m_szText;
}
m_strText = szText;
}


Expand All @@ -65,17 +49,13 @@ bool CServerTextItemPacket::Write ( NetBitStreamInterface &BitStream ) const
BitStream.Write ( m_ucShadowAlpha );

// Grab the text length
size_t sizeText = strlen ( m_szText );
if ( sizeText > 1024 )
{
sizeText = 1024;
}
size_t sizeText = Min < size_t > ( 1024, m_strText.length () );

// Write the text
BitStream.WriteCompressed ( static_cast < unsigned short > ( sizeText ) );
if ( sizeText )
{
BitStream.Write ( m_szText, sizeText );
BitStream.Write ( m_strText, sizeText );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
class CServerTextItemPacket : public CPacket
{
public:
CServerTextItemPacket ( void );
CServerTextItemPacket ( unsigned long ulUniqueId, bool bDeleteable, float fX, float fY, float fScale, const SColor color, unsigned char format, unsigned char ucShadowAlpha, char* szText );
~CServerTextItemPacket ( void );
CServerTextItemPacket ( unsigned long ulUniqueId, bool bDeleteable, float fX, float fY, float fScale, const SColor color, unsigned char format, unsigned char ucShadowAlpha, const char* szText );

inline ePacketID GetPacketID ( void ) const { return PACKET_ID_TEXT_ITEM; };
inline unsigned long GetFlags ( void ) const { return PACKET_RELIABLE | PACKET_SEQUENCED; };

bool Write ( NetBitStreamInterface& BitStream ) const;
private:
char * m_szText;
SString m_strText;
float m_fX;
float m_fY;
SColor m_Color;
Expand Down

0 comments on commit 8b51d10

Please sign in to comment.