Skip to content

Commit f960ec0

Browse files
committed
Fix some memory leaks
1 parent 6718bf3 commit f960ec0

File tree

7 files changed

+216
-66
lines changed

7 files changed

+216
-66
lines changed

Client/core/ServerBrowser/CServerCache.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,14 @@ void CServerCache::SaveServerCache(bool bWaitUntilFinished)
220220
{
221221
ms_bIsSaving = true;
222222
SetThreadPriority(hThread, THREAD_PRIORITY_LOWEST);
223-
ResumeThread(hThread);
223+
224+
if (ResumeThread(hThread) == static_cast<DWORD>(-1))
225+
{
226+
CCore::GetSingleton().GetConsole()->Printf("Could not start server cache thread.");
227+
ms_bIsSaving = false;
228+
}
229+
230+
CloseHandle(hThread);
224231
}
225232
}
226233

Client/core/ServerBrowser/CServerList.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,20 @@ void CServerListLAN::Refresh()
344344
// Create the LAN-broadcast socket
345345
if (m_Socket != INVALID_SOCKET)
346346
closesocket(m_Socket);
347+
347348
m_Socket = socket(AF_INET, SOCK_DGRAM, 0);
349+
if (m_Socket == INVALID_SOCKET)
350+
{
351+
m_strStatus = _("Cannot create LAN-broadcast socket");
352+
return;
353+
}
354+
348355
const int Flags = 1;
349-
setsockopt(m_Socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&Flags, sizeof(Flags));
350-
if (setsockopt(m_Socket, SOL_SOCKET, SO_BROADCAST, (const char*)&Flags, sizeof(Flags)) != 0)
356+
if (setsockopt(m_Socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&Flags, sizeof(Flags)) != 0 ||
357+
setsockopt(m_Socket, SOL_SOCKET, SO_BROADCAST, (const char*)&Flags, sizeof(Flags)) != 0)
351358
{
359+
closesocket(m_Socket);
360+
m_Socket = INVALID_SOCKET;
352361
m_strStatus = _("Cannot bind LAN-broadcast socket");
353362
return;
354363
}

Client/loader/SplashWindow.cpp

Lines changed: 91 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -179,55 +179,117 @@ bool Splash::CreateDeviceResources()
179179
if (!m_window)
180180
return false;
181181

182+
ReleaseDeviceResources();
183+
182184
HBITMAP backgroundResource = LoadBitmap(m_windowClass.hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
183-
HBITMAP barResource = LoadBitmap(m_windowClass.hInstance, MAKEINTRESOURCE(IDB_BITMAP2));
185+
if (backgroundResource == nullptr)
186+
return false;
184187

185-
if (backgroundResource == nullptr || barResource == nullptr)
188+
HBITMAP barResource = LoadBitmap(m_windowClass.hInstance, MAKEINTRESOURCE(IDB_BITMAP2));
189+
if (barResource == nullptr)
190+
{
191+
DeleteObject(backgroundResource);
186192
return false;
193+
}
187194

188195
HDC windowContext = GetDC(m_window);
196+
if (windowContext == nullptr)
197+
{
198+
DeleteObject(barResource);
199+
DeleteObject(backgroundResource);
200+
return false;
201+
}
189202

190-
// Background
191203
HDC sourceContext = CreateCompatibleDC(windowContext);
192-
SelectObject(sourceContext, backgroundResource);
204+
if (sourceContext == nullptr)
193205
{
194-
HBITMAP backgroundBitmap = CreateCompatibleBitmap(windowContext, m_width, m_height);
195-
HDC renderContext = CreateCompatibleDC(windowContext);
196-
SelectObject(renderContext, backgroundBitmap);
197-
198-
BITMAP source{};
199-
GetObject(backgroundResource, sizeof(source), &source);
206+
ReleaseDC(m_window, windowContext);
207+
DeleteObject(barResource);
208+
DeleteObject(backgroundResource);
209+
return false;
210+
}
200211

201-
SetStretchBltMode(renderContext, HALFTONE);
202-
StretchBlt(renderContext, 0, 0, m_width, m_height, sourceContext, 0, 0, source.bmWidth, source.bmHeight, SRCCOPY);
212+
HBITMAP bgBitmap{};
213+
HDC bgContext{};
214+
HBITMAP barBitmap{};
215+
HDC barContext{};
216+
HRGN barRegion{};
217+
bool success = false;
203218

204-
m_bgBitmap = backgroundBitmap;
205-
m_bgContext = renderContext;
206-
}
219+
BITMAP source{};
207220

208-
// Loading bar
209-
SelectObject(sourceContext, barResource);
221+
do
210222
{
211-
HBITMAP barBitmap = CreateCompatibleBitmap(windowContext, m_barWidth, m_barHeight);
212-
HDC renderContext = CreateCompatibleDC(windowContext);
213-
SelectObject(renderContext, barBitmap);
223+
SelectObject(sourceContext, backgroundResource);
224+
225+
bgBitmap = CreateCompatibleBitmap(windowContext, m_width, m_height);
226+
if (bgBitmap == nullptr)
227+
break;
214228

215-
BITMAP source{};
216-
GetObject(barResource, sizeof(source), &source);
229+
bgContext = CreateCompatibleDC(windowContext);
230+
if (bgContext == nullptr)
231+
break;
217232

218-
SetStretchBltMode(renderContext, HALFTONE);
219-
StretchBlt(renderContext, 0, 0, m_barWidth, m_barHeight, sourceContext, 0, 0, source.bmWidth, source.bmHeight, SRCCOPY);
233+
SelectObject(bgContext, bgBitmap);
220234

221-
m_barBitmap = barBitmap;
222-
m_barContext = renderContext;
223-
m_barRegion = CreateRectRgn(0, m_barY, m_width, m_barY + m_barHeight);
224-
}
235+
if (!GetObject(backgroundResource, sizeof(source), &source))
236+
break;
237+
238+
SetStretchBltMode(bgContext, HALFTONE);
239+
if (!StretchBlt(bgContext, 0, 0, m_width, m_height, sourceContext, 0, 0, source.bmWidth, source.bmHeight, SRCCOPY))
240+
break;
241+
242+
SelectObject(sourceContext, barResource);
243+
244+
barBitmap = CreateCompatibleBitmap(windowContext, m_barWidth, m_barHeight);
245+
if (barBitmap == nullptr)
246+
break;
247+
248+
barContext = CreateCompatibleDC(windowContext);
249+
if (barContext == nullptr)
250+
break;
251+
252+
SelectObject(barContext, barBitmap);
253+
254+
if (!GetObject(barResource, sizeof(source), &source))
255+
break;
256+
257+
SetStretchBltMode(barContext, HALFTONE);
258+
if (!StretchBlt(barContext, 0, 0, m_barWidth, m_barHeight, sourceContext, 0, 0, source.bmWidth, source.bmHeight, SRCCOPY))
259+
break;
260+
261+
barRegion = CreateRectRgn(0, m_barY, m_width, m_barY + m_barHeight);
262+
if (barRegion == nullptr)
263+
break;
264+
265+
success = true;
266+
} while (false);
225267

226268
DeleteDC(sourceContext);
269+
ReleaseDC(m_window, windowContext);
227270
DeleteObject(barResource);
228271
DeleteObject(backgroundResource);
229272

230-
ReleaseDC(m_window, windowContext);
273+
if (!success)
274+
{
275+
if (barRegion)
276+
DeleteObject(barRegion);
277+
if (barContext)
278+
DeleteDC(barContext);
279+
if (barBitmap)
280+
DeleteObject(barBitmap);
281+
if (bgContext)
282+
DeleteDC(bgContext);
283+
if (bgBitmap)
284+
DeleteObject(bgBitmap);
285+
return false;
286+
}
287+
288+
m_bgBitmap = bgBitmap;
289+
m_bgContext = bgContext;
290+
m_barBitmap = barBitmap;
291+
m_barContext = barContext;
292+
m_barRegion = barRegion;
231293
return true;
232294
}
233295

@@ -521,26 +583,6 @@ void SplashThread::PostRun()
521583
//
522584
///////////////////////////////////////////////////////////////////////////
523585

524-
#ifdef MTA_DEBUG
525-
526-
void ShowSplash(HINSTANCE instance)
527-
{
528-
}
529-
530-
void HideSplash()
531-
{
532-
}
533-
534-
void SuspendSplash()
535-
{
536-
}
537-
538-
void ResumeSplash()
539-
{
540-
}
541-
542-
#else
543-
544586
void ShowSplash(HINSTANCE instance)
545587
{
546588
if (g_splashThread.Exists())
@@ -569,8 +611,6 @@ void ResumeSplash()
569611
g_splash.Show();
570612
}
571613

572-
#endif
573-
574614
///////////////////////////////////////////////////////////////////////////
575615
//
576616
// Utility functions.

Client/mods/deathmatch/logic/CBassAudio.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,16 @@ bool CBassAudio::BeginLoadingMedia()
153153
m_pVars = new SSoundThreadVariables();
154154
m_pVars->strURL = m_strPath;
155155
m_pVars->lFlags = lFlags;
156-
CreateThread(NULL, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(&CBassAudio::PlayStreamIntern), m_uiCallbackId, 0, NULL);
156+
HANDLE hThread = CreateThread(NULL, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(&CBassAudio::PlayStreamIntern), m_uiCallbackId, 0, NULL);
157+
if (!hThread)
158+
{
159+
g_pCore->GetConsole()->Printf("Could not create audio stream thread for %s", *m_strPath);
160+
delete m_pVars;
161+
m_pVars = nullptr;
162+
return false;
163+
}
164+
165+
CloseHandle(hThread);
157166
m_bPendingPlay = true;
158167
OutputDebugLine("[Bass] stream connect started");
159168
}

Client/mods/deathmatch/logic/CClientSound.cpp

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <StdInc.h>
1111
#include "CBassAudio.h"
12+
#include <memory>
13+
#include <utility>
1214

1315
CClientSound::CClientSound(CClientManager* pManager, ElementID ID) : ClassInit(this), CClientEntity(ID)
1416
{
@@ -25,8 +27,9 @@ CClientSound::CClientSound(CClientManager* pManager, ElementID ID) : ClassInit(t
2527
m_fPlaybackSpeed = 1.0f;
2628
m_bPan = true;
2729
m_fPan = 0.0f;
30+
m_bThrottle = false;
2831

29-
m_pBuffer = nullptr;
32+
m_uiBufferLength = 0;
3033
m_uiFrameNumberCreated = g_pClientGame->GetFrameCount();
3134
}
3235

@@ -35,8 +38,38 @@ CClientSound::~CClientSound()
3538
Destroy();
3639
m_pSoundManager->RemoveFromList(this);
3740

38-
delete m_pBuffer;
39-
m_pBuffer = NULL;
41+
ReleaseBuffer();
42+
}
43+
44+
void CClientSound::ReleaseBuffer()
45+
{
46+
if (!m_Buffer)
47+
return;
48+
49+
m_Buffer.reset();
50+
m_uiBufferLength = 0;
51+
}
52+
53+
// Pull an existing audio buffer so it's owned by the sound; pass the matching deleter so we can release it safely later.
54+
void CClientSound::AdoptBuffer(void* pMemory, unsigned int uiLength, AudioBufferDeleter deleter)
55+
{
56+
if (!pMemory || uiLength == 0)
57+
{
58+
ReleaseBuffer();
59+
return;
60+
}
61+
62+
if (!m_Buffer || m_Buffer.get() != pMemory)
63+
{
64+
m_Buffer = BufferPtr(pMemory, std::move(deleter));
65+
}
66+
else
67+
{
68+
m_Buffer.get_deleter() = std::move(deleter);
69+
}
70+
71+
m_uiBufferLength = uiLength;
72+
m_strPath.clear();
4073
}
4174

4275
////////////////////////////////////////////////////////////
@@ -112,16 +145,20 @@ bool CClientSound::Create()
112145
return false;
113146

114147
// Initial state
115-
if (!m_pBuffer)
148+
if (!m_Buffer)
116149
m_pAudio = new CBassAudio(m_bStream, m_strPath, m_bLoop, m_bThrottle, m_b3D);
117150
else
118-
m_pAudio = new CBassAudio(m_pBuffer, m_uiBufferLength, m_bLoop, m_b3D);
151+
m_pAudio = new CBassAudio(m_Buffer.get(), m_uiBufferLength, m_bLoop, m_b3D);
119152

120153
m_bDoneCreate = true;
121154

122155
// Load file/start connect
123156
if (!m_pAudio->BeginLoadingMedia())
157+
{
158+
delete m_pAudio;
159+
m_pAudio = nullptr;
124160
return false;
161+
}
125162

126163
// Get and save length
127164
m_dLength = m_pAudio->GetLength();
@@ -230,10 +267,13 @@ bool CClientSound::Play(const SString& strPath, bool bLoop)
230267
{
231268
assert(m_strPath.empty());
232269

270+
ReleaseBuffer();
271+
233272
m_bStream = false;
234273
m_b3D = false;
235274
m_strPath = strPath;
236275
m_bLoop = bLoop;
276+
m_bThrottle = false;
237277
m_bPan = false;
238278

239279
// Instant distance-stream in
@@ -246,9 +286,9 @@ bool CClientSound::Play(void* pMemory, unsigned int uiLength, bool bLoop)
246286

247287
m_bStream = false;
248288
m_b3D = false;
249-
m_pBuffer = pMemory;
250-
m_uiBufferLength = uiLength;
289+
AdoptBuffer(pMemory, uiLength, AudioBufferDeleter::ForNewArray());
251290
m_bLoop = bLoop;
291+
m_bThrottle = false;
252292
m_bPan = false;
253293

254294
// Instant distance-stream in
@@ -259,10 +299,13 @@ bool CClientSound::Play3D(const SString& strPath, bool bLoop)
259299
{
260300
assert(m_strPath.empty());
261301

302+
ReleaseBuffer();
303+
262304
m_bStream = false;
263305
m_b3D = true;
264306
m_strPath = strPath;
265307
m_bLoop = bLoop;
308+
m_bThrottle = false;
266309

267310
BeginSimulationOfPlayPosition();
268311

@@ -273,9 +316,9 @@ bool CClientSound::Play3D(void* pMemory, unsigned int uiLength, bool bLoop)
273316
{
274317
m_bStream = false;
275318
m_b3D = true;
276-
m_pBuffer = pMemory;
277-
m_uiBufferLength = uiLength;
319+
AdoptBuffer(pMemory, uiLength, AudioBufferDeleter::ForNewArray());
278320
m_bLoop = bLoop;
321+
m_bThrottle = false;
279322

280323
BeginSimulationOfPlayPosition();
281324

@@ -286,6 +329,8 @@ void CClientSound::PlayStream(const SString& strURL, bool bLoop, bool bThrottle,
286329
{
287330
assert(m_strPath.empty());
288331

332+
ReleaseBuffer();
333+
289334
m_bStream = true;
290335
m_b3D = b3D;
291336
m_strPath = strURL;

0 commit comments

Comments
 (0)