Skip to content

Commit 8b51d10

Browse files
committed
Fixed a couple of memory leaks.
Replaced some char array allocations with strings.
1 parent 66207e1 commit 8b51d10

File tree

10 files changed

+27
-104
lines changed

10 files changed

+27
-104
lines changed

MTA10/mods/deathmatch/logic/CServer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ char* szServerErrors[4] =
4444
CServer::CServer ( void )
4545
{
4646
assert ( !g_bIsStarted );
47-
m_szConfig = NULL;
4847

4948
// Initialize
5049
m_bIsReady = false;
@@ -133,8 +132,7 @@ bool CServer::Start ( const char* szConfig )
133132
// Not already started?
134133
if ( !g_bIsStarted )
135134
{
136-
m_szConfig = new char [ strlen ( szConfig ) + 1 ];
137-
strcpy ( m_szConfig, szConfig );
135+
m_strConfig = szConfig;
138136

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

304302
char szArgument4 [64];
305-
strcpy ( szArgument4, m_szConfig );
303+
strcpy ( szArgument4, m_strConfig );
306304

307305
char szArgument5 [8];
308306
strcpy ( szArgument5, "-s" );

MTA10/mods/deathmatch/logic/CServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CServer
5252
CCriticalSection m_CriticalSection;
5353
char m_szServerRoot [MAX_PATH];
5454
SString m_strDLLFile;
55-
char* m_szConfig;
55+
SString m_strConfig;
5656

5757
int m_iLastError;
5858

MTA10/mods/deathmatch/logic/rpc/CInputRPCs.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,14 @@ void CInputRPCs::ForceReconnect ( NetBitStreamInterface& bitStream )
268268
if ( szPassword )
269269
{
270270
g_pCore->Reconnect ( szHost, usPort, szPassword );
271+
delete [] szHost;
271272
return;
272273
}
273274
}
274275

275276
g_pCore->Reconnect ( szHost, usPort, NULL );
276277
}
278+
delete [] szHost;
277279
}
278280
}
279281

MTA10_Server/mods/deathmatch/logic/CPlayerTextManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void CPlayerTextManager::Process ()
153153
// Tell the player
154154
m_pPlayer->Send ( CServerTextItemPacket( textItem->m_ulUniqueId, textItem->m_bDeletable, textItem->m_vecPosition.fX,
155155
textItem->m_vecPosition.fY, textItem->m_fScale,
156-
textItem->m_Color, textItem->m_ucFormat, textItem->m_ucShadowAlpha, textItem->m_szText ) );
156+
textItem->m_Color, textItem->m_ucFormat, textItem->m_ucShadowAlpha, textItem->m_strText ) );
157157

158158
// Delete the text item created on the queue
159159
delete textItem;

MTA10_Server/mods/deathmatch/logic/CRegistry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct CRegistryResultCell
8080
~CRegistryResultCell ( void )
8181
{
8282
if ( pVal )
83-
delete pVal;
83+
delete [] pVal;
8484
}
8585

8686
int nType; // Type identifier, SQLITE_*

MTA10_Server/mods/deathmatch/logic/CTextItem.cpp

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,15 @@
1212

1313
#include "StdInc.h"
1414

15-
unsigned long ulUniqueId = 0;
15+
static unsigned long ulUniqueId = 0;
1616

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

2222
// Copy the text
23-
if ( szText )
24-
{
25-
m_szText = new char [ strlen ( szText ) + 1];
26-
strcpy ( m_szText, szText );
27-
}
28-
else
29-
{
30-
m_szText = new char [1];
31-
m_szText [0] = 0;
32-
}
23+
m_strText = szText ? szText : "";
3324

3425
// Assign the properties
3526
m_vecPosition = vecPosition;
@@ -45,16 +36,7 @@ CTextItem::CTextItem ( const char* szText, const CVector2D& vecPosition, eTextPr
4536
CTextItem::CTextItem ( const CTextItem& TextItem )
4637
{
4738
// Copy the tex
48-
if ( TextItem.m_szText )
49-
{
50-
m_szText = new char [strlen ( TextItem.m_szText ) + 1];
51-
strcpy ( m_szText, TextItem.m_szText );
52-
}
53-
else
54-
{
55-
m_szText = new char [1];
56-
m_szText [0] = 0;
57-
}
39+
m_strText = TextItem.m_strText;
5840

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

7153
bool CTextItem::operator= ( const CTextItem& TextItem )
7254
{
73-
// Delete the previous title buffer and copy the buffer in the text item given
74-
if ( m_szText ) delete [] m_szText;
75-
76-
if ( TextItem.m_szText )
77-
{
78-
m_szText = new char [strlen ( TextItem.m_szText ) + 1];
79-
strcpy ( m_szText, TextItem.m_szText );
80-
}
81-
else
82-
{
83-
m_szText = new char [1];
84-
m_szText [0] = 0;
85-
}
55+
// Copy the new text
56+
m_strText = TextItem.m_strText;
8657

8758
// Copy the properties
8859
m_ulUniqueId = TextItem.m_ulUniqueId;
@@ -104,13 +75,6 @@ CTextItem::~CTextItem ( )
10475
// Tell all our observers about it
10576
m_bDeletable = true;
10677
NotifyObservers ();
107-
108-
// Delete our text
109-
if ( m_szText )
110-
{
111-
delete [] m_szText;
112-
m_szText = NULL;
113-
}
11478
}
11579

11680

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

138102
void CTextItem::SetText ( const char* szText )
139103
{
140-
// If the text is the same, don't bother
141-
if ( m_szText && strcmp ( m_szText, szText ) == 0 )
104+
// If the text has changed
105+
if ( m_strText != szText )
142106
{
143-
return;
144-
}
107+
// Update
108+
m_strText = szText;
145109

146-
// Delete the previous buffer if neccessary
147-
if ( m_szText )
148-
{
149-
delete [] m_szText;
110+
// Tell the text displays
111+
NotifyObservers ();
150112
}
151-
152-
// Allocate and copy the new text
153-
m_szText = new char [strlen ( szText ) + 1];
154-
strcpy ( m_szText, szText );
155-
156-
// Tell the text displays
157-
NotifyObservers ();
158-
}
159-
160-
161-
char* CTextItem::GetText ( char* pBuffer, size_t bufferSize )
162-
{
163-
strncpy ( pBuffer, m_szText, bufferSize );
164-
return pBuffer;
165113
}
166114

167115

@@ -193,7 +141,6 @@ void CTextItem::SetColor ( const SColor color )
193141
}
194142

195143

196-
197144
void CTextItem::SetScale ( float fScale )
198145
{
199146
// If the scale has changed

MTA10_Server/mods/deathmatch/logic/CTextItem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CTextItem
3939
bool operator= ( const CTextItem& TextItem );
4040

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

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

6262
private:
63-
char * m_szText;
63+
SString m_strText;
6464
CVector2D m_vecPosition;
6565
SColor m_Color;
6666
float m_fScale;

MTA10_Server/mods/deathmatch/logic/luadefs/CLuaTextDefs.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,7 @@ int CLuaTextDefs::textItemGetText ( lua_State* luaVM )
421421
CTextItem* pTextItem = lua_totextitem ( luaVM, 1 );
422422
if ( pTextItem )
423423
{
424-
char szBuffer[1024];
425-
pTextItem->GetText ( szBuffer, 1024 );
426-
lua_pushstring ( luaVM, szBuffer );
424+
lua_pushstring ( luaVM, pTextItem->GetText() );
427425
return 1;
428426
}
429427
}

MTA10_Server/mods/deathmatch/logic/packets/CServerTextItemPacket.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@
1313

1414
#include "StdInc.h"
1515

16-
CServerTextItemPacket::CServerTextItemPacket( void )
17-
{
18-
m_szText = NULL;
19-
}
20-
21-
22-
CServerTextItemPacket::CServerTextItemPacket( unsigned long ulUniqueId, bool bDeleteable, float fX, float fY, float fScale, const SColor color, unsigned char format, unsigned char ucShadowAlpha, char* szText )
16+
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 )
2317
{
2418
m_ulUniqueId = ulUniqueId;
2519
m_bDeletable = bDeleteable;
@@ -29,17 +23,7 @@ CServerTextItemPacket::CServerTextItemPacket( unsigned long ulUniqueId, bool bDe
2923
m_Color = color;
3024
m_ucFormat = format;
3125
m_ucShadowAlpha = ucShadowAlpha;
32-
m_szText = new char [strlen ( szText ) + 1];
33-
strcpy ( m_szText, szText );
34-
}
35-
36-
37-
CServerTextItemPacket::~CServerTextItemPacket ( void )
38-
{
39-
if ( m_szText )
40-
{
41-
delete [] m_szText;
42-
}
26+
m_strText = szText;
4327
}
4428

4529

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

6751
// Grab the text length
68-
size_t sizeText = strlen ( m_szText );
69-
if ( sizeText > 1024 )
70-
{
71-
sizeText = 1024;
72-
}
52+
size_t sizeText = Min < size_t > ( 1024, m_strText.length () );
7353

7454
// Write the text
7555
BitStream.WriteCompressed ( static_cast < unsigned short > ( sizeText ) );
7656
if ( sizeText )
7757
{
78-
BitStream.Write ( m_szText, sizeText );
58+
BitStream.Write ( m_strText, sizeText );
7959
}
8060
}
8161

MTA10_Server/mods/deathmatch/logic/packets/CServerTextItemPacket.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919
class CServerTextItemPacket : public CPacket
2020
{
2121
public:
22-
CServerTextItemPacket ( void );
23-
CServerTextItemPacket ( unsigned long ulUniqueId, bool bDeleteable, float fX, float fY, float fScale, const SColor color, unsigned char format, unsigned char ucShadowAlpha, char* szText );
24-
~CServerTextItemPacket ( void );
22+
CServerTextItemPacket ( unsigned long ulUniqueId, bool bDeleteable, float fX, float fY, float fScale, const SColor color, unsigned char format, unsigned char ucShadowAlpha, const char* szText );
2523

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

2927
bool Write ( NetBitStreamInterface& BitStream ) const;
3028
private:
31-
char * m_szText;
29+
SString m_strText;
3230
float m_fX;
3331
float m_fY;
3432
SColor m_Color;

0 commit comments

Comments
 (0)