Skip to content

Commit 988c3d0

Browse files
committed
Add global handler for std::bad_alloc
These exceptions will be represented by exception code 0xE03B10C0
1 parent 4b9e687 commit 988c3d0

File tree

13 files changed

+116
-1
lines changed

13 files changed

+116
-1
lines changed

Client/cefweb/CefWeb.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extern "C" _declspec(dllexport) CWebCoreInterface* InitWebCoreInterface(CCoreInt
2222
// Ensure main thread identification is consistent
2323
IsMainThread();
2424

25+
SetMemoryAllocationFailureHandler();
26+
2527
CWebCore* pWebCore = new CWebCore;
2628
return pWebCore;
2729
}

Client/core/CCommandFuncs.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,19 @@ void CCommandFuncs::Test(const char* szParameters)
427427
}
428428
}
429429
}
430+
#if defined(MTA_DEBUG) || MTASA_VERSION_TYPE == VERSION_TYPE_CUSTOM
431+
else if (SStringX(szParameters) == "bad_alloc")
432+
{
433+
if (FileExists(CalcMTASAPath("debug.txt")))
434+
{
435+
while (true)
436+
{
437+
new int[100 * 1024 * 1024]();
438+
g_pCore->GetConsole()->Print("Allocated 100 MiB");
439+
}
440+
}
441+
}
442+
#endif
430443
}
431444

432445
void CCommandFuncs::Serial(const char* szParameters)

Client/core/CCrashDumpWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ void CCrashDumpWriter::DumpCoreLog(CExceptionInformation* pExceptionInformation)
291291

292292
// For the crash dialog
293293
SetApplicationSetting("diagnostics", "last-crash-info", strInfo);
294+
SetApplicationSetting("diagnostics", "last-crash-module", pExceptionInformation->GetModulePathName());
295+
SetApplicationSettingInt("diagnostics", "last-crash-code", pExceptionInformation->GetCode());
294296
WriteDebugEvent(strInfo.Replace("\n", " "));
295297
}
296298
}

Client/core/Core.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "profiler/SharedUtil.Profiler.h"
1616
#define UTF8_FILE_HOOKS_PERSONALITY_Core
1717
#include "SharedUtil.Win32Utf8FileHooks.hpp"
18+
#include "SharedUtil.Memory.h"
1819

1920
#define CORE_API extern "C" __declspec(dllexport)
2021

@@ -94,6 +95,8 @@ CORE_API int InitializeCore()
9495

9596
WriteDebugEvent(SString("ModuleFileName: %s", *GetLaunchPathFilename()));
9697

98+
SetMemoryAllocationFailureHandler();
99+
97100
g_pCore = new CCore();
98101
return 0;
99102
}

Client/game_sa/gamesa_init.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <net/CNet.h>
1515
#include "gamesa_init.h"
1616
#include "CGameSA.h"
17+
#include "SharedUtil.Memory.h"
1718
#define DECLARE_PROFILER_SECTION_gamesa_init
1819
#include "profiler/SharedUtil.Profiler.h"
1920

@@ -30,6 +31,8 @@ MTAEXPORT CGame* GetGameInterface(CCoreInterface* pCore)
3031
g_pNet = pCore->GetNetwork();
3132
assert(g_pNet);
3233

34+
SetMemoryAllocationFailureHandler();
35+
3336
pGame = new CGameSA;
3437
g_pCore = pCore;
3538

Client/gui/Main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ MTAEXPORT CGUI* InitGUIInterface(IDirect3DDevice9* pDevice)
3131
// Create our GUI interface if not already done
3232
if (!g_pGUI)
3333
{
34+
SetMemoryAllocationFailureHandler();
3435
g_pGUI = new CGUI_Impl(pDevice);
3536
}
3637

Client/loader-proxy/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ BOOL OnLibraryAttach()
117117

118118
VOID OnGameLaunch()
119119
{
120+
SetMemoryAllocationFailureHandler();
121+
120122
std::error_code ec{};
121123

122124
// MTA:SA launches GTA:SA process with the GTA:SA installation directory as the current directory.

Client/loader/CInstallManager.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "GameExecutablePatcher.h"
1717
#include "FileGenerator.h"
1818
#include "FileSystem.h"
19+
#include "SharedUtil.Memory.h"
1920

2021
namespace fs = std::filesystem;
2122

@@ -418,9 +419,10 @@ SString CInstallManager::_ShowCrashFailDialog()
418419
SetApplicationSetting("diagnostics", "gta-fopen-fail", GetApplicationSetting("diagnostics", "gta-fopen-last"));
419420
SetApplicationSetting("diagnostics", "gta-fopen-last", "");
420421

421-
SString strMessage = GetApplicationSetting("diagnostics", "last-crash-info");
422422
SString strReason = GetApplicationSetting("diagnostics", "last-crash-reason");
423423
SetApplicationSetting("diagnostics", "last-crash-reason", "");
424+
425+
SString strMessage = GetApplicationSetting("diagnostics", "last-crash-info");
424426
if (strReason == "direct3ddevice-reset")
425427
{
426428
strMessage += _("** The crash was caused by a graphics driver error **\n\n** Please update your graphics drivers **");
@@ -430,6 +432,15 @@ SString CInstallManager::_ShowCrashFailDialog()
430432
strMessage += strReason;
431433
}
432434

435+
const SString moduleName = GetApplicationSetting("diagnostics", "last-crash-module");
436+
const int exceptionCode = GetApplicationSettingInt("diagnostics", "last-crash-code");
437+
438+
if (exceptionCode == CUSTOM_EXCEPTION_CODE_OOM && moduleName.EndsWithI("\\kernelbase.dll"))
439+
{
440+
strMessage += '\n';
441+
strMessage += _("** Out of memory - this crash was caused by insufficient free or fragmented memory. **");
442+
}
443+
433444
strMessage = strMessage.Replace("\r", "").Replace("\n", "\r\n");
434445
SString strResult = ShowCrashedDialog(g_hInstance, strMessage);
435446
HideCrashedDialog();

Client/loader/MainFunctions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ void PreLaunchWatchDogs()
568568
WatchDogBeginSection("L1"); // Gets closed when online game has started
569569
SetApplicationSetting("diagnostics", "gta-fopen-fail", "");
570570
SetApplicationSetting("diagnostics", "last-crash-reason", "");
571+
SetApplicationSetting("diagnostics", "last-crash-module", "");
572+
SetApplicationSetting("diagnostics", "last-crash-code", "");
571573
SetApplicationSetting("diagnostics", "gta-fopen-last", "");
572574
}
573575

Client/mods/deathmatch/Client.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*****************************************************************************/
1111

1212
#include "StdInc.h"
13+
#include "SharedUtil.Memory.h"
1314

1415
CClient* g_pClient = NULL;
1516

@@ -18,6 +19,7 @@ MTAEXPORT CClientBase* __cdecl InitClient(void)
1819
// Eventually create a client base interface
1920
if (!g_pClient)
2021
{
22+
SetMemoryAllocationFailureHandler();
2123
g_pClient = new CClient;
2224
}
2325
return g_pClient;

0 commit comments

Comments
 (0)