From 0f5c5dcf3eae75eb0ba92b8687a2f20d4581319d Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 16 Mar 2026 16:42:30 +0100 Subject: [PATCH] Fix memory issues on Web --- Source/Engine/Platform/Unix/UnixPlatform.h | 1 - Source/Engine/Platform/Web/WebPlatform.cpp | 11 +---------- Source/Engine/Platform/Web/WebPlatform.h | 5 ++++- Source/Engine/Serialization/Json.h | 2 +- Source/Tools/Flax.Build/Platforms/Web/WebToolchain.cs | 11 +++++++++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Source/Engine/Platform/Unix/UnixPlatform.h b/Source/Engine/Platform/Unix/UnixPlatform.h index d0f5ce481..f393b5914 100644 --- a/Source/Engine/Platform/Unix/UnixPlatform.h +++ b/Source/Engine/Platform/Unix/UnixPlatform.h @@ -12,7 +12,6 @@ class FLAXENGINE_API UnixPlatform : public PlatformBase { public: - // [PlatformBase] static void* Allocate(uint64 size, uint64 alignment); static void Free(void* ptr); diff --git a/Source/Engine/Platform/Web/WebPlatform.cpp b/Source/Engine/Platform/Web/WebPlatform.cpp index 6470a26f7..9967dd93f 100644 --- a/Source/Engine/Platform/Web/WebPlatform.cpp +++ b/Source/Engine/Platform/Web/WebPlatform.cpp @@ -179,15 +179,6 @@ void WebPlatform::Log(const StringView& msg, int32 logType) emscripten_log(flags, buffer); } -#if !BUILD_RELEASE - -bool WebPlatform::IsDebuggerPresent() -{ - return false; -} - -#endif - String WebPlatform::GetComputerName() { return TEXT("Web"); @@ -335,7 +326,7 @@ Array WebPlatform::GetStackFrames(int32 skipCount, int Array result; #if CRASH_LOG_ENABLE // Get callstack - char callstack[1024]; + char callstack[2000]; emscripten_get_callstack(EM_LOG_JS_STACK, callstack, sizeof(callstack)); // Parse callstack diff --git a/Source/Engine/Platform/Web/WebPlatform.h b/Source/Engine/Platform/Web/WebPlatform.h index 83d6f0e15..4a6b85ac9 100644 --- a/Source/Engine/Platform/Web/WebPlatform.h +++ b/Source/Engine/Platform/Web/WebPlatform.h @@ -111,7 +111,10 @@ public: static void GetUTCTime(int32& year, int32& month, int32& dayOfWeek, int32& day, int32& hour, int32& minute, int32& second, int32& millisecond); static void Log(const StringView& msg, int32 logType = 1); #if !BUILD_RELEASE - static bool IsDebuggerPresent(); + static bool IsDebuggerPresent() + { + return false; + } #endif static String GetComputerName(); static bool GetHasFocus(); diff --git a/Source/Engine/Serialization/Json.h b/Source/Engine/Serialization/Json.h index 6f300f3ae..1823935e5 100644 --- a/Source/Engine/Serialization/Json.h +++ b/Source/Engine/Serialization/Json.h @@ -5,7 +5,7 @@ #include "Engine/Core/Types/String.h" #include "Engine/Core/Types/StringView.h" -#if PLATFORM_WEB +#if PLATFORM_WEB && PLATFORM_SIMD_SSE2 #define RAPIDJSON_SSE2 #elif PLATFORM_SIMD_SSE4_2 #define RAPIDJSON_SSE42 diff --git a/Source/Tools/Flax.Build/Platforms/Web/WebToolchain.cs b/Source/Tools/Flax.Build/Platforms/Web/WebToolchain.cs index 26eb039d1..c3e8f7749 100644 --- a/Source/Tools/Flax.Build/Platforms/Web/WebToolchain.cs +++ b/Source/Tools/Flax.Build/Platforms/Web/WebToolchain.cs @@ -169,8 +169,10 @@ namespace Flax.Build.Platforms args.Add("-fsanitize=address"); if (sanitizers.HasFlag(Sanitizer.Undefined)) args.Add("-fsanitize=undefined"); - if (sanitizers == Sanitizer.None) - args.Add("-fsanitize=null -fsanitize-minimal-runtime"); // Minimal Runtime + //if (sanitizers == Sanitizer.None && options.Configuration != TargetConfiguration.Release) + // args.Add("-fsanitize=null -fsanitize-minimal-runtime"); // Minimal Runtime + if (sanitizers == Sanitizer.None) // TODO: fix random memory issues around malloc (eg. when resizing canvas) that are not happening when using Address sanitizer (wierd) + args.Add("-fsanitize=address"); if (Configuration.WebThreads) args.Add("-pthread"); @@ -289,7 +291,12 @@ namespace Flax.Build.Platforms initialMemory = Math.Max(initialMemory, 64); // Address Sanitizer needs more memory args.Add($"-sINITIAL_MEMORY={initialMemory}MB"); args.Add("-sSTACK_SIZE=4MB"); + args.Add("-sASYNCIFY_STACK_SIZE=8192"); args.Add("-sALLOW_MEMORY_GROWTH=1"); + //args.Add("-sSAFE_HEAP=1"); + args.Add("-sABORTING_MALLOC=0"); + //args.Add("-sMALLOC=emmalloc-memvalidate"); + //args.Add("-sMALLOC=emmalloc"); // Setup file access (Game Cooker packs files with file_packager tool) args.Add("-sFORCE_FILESYSTEM");