SDL2.dll built with DWARF-5 or DWARF-4 debug info causes runtime errors at Heaptrc leak dump
## Summary
The renowned [SDL2 multimedia library](libsdl.org) supports the use of a custom allocator by setting it with the [`SDL_SetMemoryFunctions()`](https://wiki.libsdl.org/SDL2/SDL_SetMemoryFunctions). Unfortunately, if employing Heaptrc here, an SDL2 leak may lead to unjustified runtime error at program exit when the `SDL2.dll` file contains DWARF debug info.
## What is the current bug behavior?
- DWARF-5 causes `RunError(998)`.
- DWARF-4 causes `RunError(131)`.
- DWARF-3 causes no error - the program successfully terminates, reporting the leak.
## System Information
- **Operating system:** Windows 7 Ultimate x64
- **Processor architecture:** x86
- **Environment version:** Lazarus 4.2 (rev lazarus_4_2) FPC 3.2.2 x86_64-win64-win32/win64
- **Device:** Computer
## Example Project
[fpc_dwarf_crash.7z](/uploads/ad73a1a0df017ef9b52ea8483048d4c2/fpc_dwarf_crash.7z)
Contains pre-built binaries that may save someone's time. Read on to find out how to craft them yourself.
## Relevant logs and/or screenshots
See the attached archive.
## Steps to reproduce
1. Get GCC 15.2.0 that is able to compile for Windows. I've managed to reproduce this issue with mingw-w64 build from [WinLibs.com](https://www.winlibs.com/#download-release).
[GCC 15.2.0 (with POSIX threads) + MinGW-w64 13.0.0 (MSVCRT) - release 5](https://github.com/brechtsanders/winlibs_mingw/releases/download/15.2.0posix-13.0.0-msvcrt-r5/winlibs-i686-posix-dwarf-gcc-15.2.0-mingw-w64msvcrt-13.0.0-r5.7z)
However, the problem was initially noticed with `i686-w64-mingw32-gcc-wrapper-14.3.0` used in our Nix-based CI/CD.
It is also worth noting that I first tried to use my old GCC 8.1.0 ([`i686-8.1.0-win32-dwarf-rt_v6-rev0`](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/8.1.0/threads-win32/dwarf/)) to reproduce that behavior, but `SDL2.dll` with DWARF-4 from it worked without any problems for some reason (and DWARF-5 wasn't fully supported back then, producing a corrupt DLL image).
2. Download sources of SDL2 version 2.32.6: https://libsdl.org/release/SDL2-2.32.6.zip.
The others may also be suitable, but I haven't checked.
3. Run `mingwvars.bat` in command prompt and start `cmake-gui` from it. Then generate MinGW Makefiles for SDL2 sources, specifying the following stuff during configure phase:
- `CMAKE_BUILD_TYPE = RelWithDebInfo`
- `CMAKE_C_FLAGS_RELWITHDEBINFO` - change `-g` here to `-gdwarf-5` or `-gdwarf-4`, whatever you need to test
I used CMake 3.27.0 and [CodeBlocks extra generator](https://cmake.org/cmake/help/v3.27/manual/cmake-generators.7.html#extra-generators). Also, disabling `SDL_STATIC` could reduce your compilation time.
4. Build `SDL2` target to obtain the resulting `SDL2.dll`.
Place it near the `fpc_dwarf_crash.lpi` project, build it in Lazarus, launch and observe the runtime error.
issue