Hey guys, I'm using --gc:arc and -d:useMalloc, but I'm getting memory leak warnings with crtdbg.h:
proc setCrtDbgMemFlags()
{.header:"#include <crtdbg.h>\n#include <stdlib.h>"
importcpp:"_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF)".}
template enableMemLeakLogging*() =
{.emit:"""/*INCLUDESECTION*/
#define _CRTDBG_MAP_ALLOC
""".}
setCrtDbgMemFlags()
type Test = ref object
proc main() =
var test = Test()
while true:
discard
test = nil
when not defined(release):
enableMemLeakLogging()
main()
Seemed like a false positive. I don't get leak warnings if I don't use -d:useMalloc, but I need that switch to avoid issues with my DLLs.
I tried vld from https://github.com/oneiric/vld to get a decent trace of the "leak", and I get this output
Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.7.0 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x0000000014E7D010: 9 bytes ----------
Leak Hash: 0x3B5FDA4C, Count: 1, Total 9 bytes
Call Stack (TID 38260):
ucrtbased.dll!calloc()
C:\src\ws-worldengine\feature\hcr\nimcache\stdlib_system.nim.cpp (560): main.exe!alloc0Impl__KzdpcuLT9aef9bsiSHlIu9aFg_2() + 0x10 bytes
C:\src\ws-worldengine\feature\hcr\nimcache\stdlib_system.nim.cpp (568): main.exe!allocShared0Impl__KzdpcuLT9aef9bsiSHlIu9aFg() + 0xA bytes
C:\src\ws-worldengine\feature\hcr\nimcache\stdlib_system.nim.cpp (2182): main.exe!alignedAlloc0__tDURxGxuLs0hKEDbtrBxug() + 0xD bytes
C:\src\ws-worldengine\feature\hcr\nimcache\stdlib_system.nim.cpp (2258): main.exe!nimNewObj() + 0x12 bytes
C:\src\ws-worldengine\feature\hcr\nimcache\@mmain.nim.cpp (259): main.exe!main__9bQIWt54CdTNMd7hgR2Bl9cw() + 0xF bytes
C:\src\ws-worldengine\feature\hcr\nimcache\@mmain.nim.cpp (322): main.exe!NimMainModule()
C:\src\ws-worldengine\feature\hcr\nimcache\@mmain.nim.cpp (297): main.exe!NimMainInner()
C:\src\ws-worldengine\feature\hcr\nimcache\@mmain.nim.cpp (305): main.exe!NimMain()
C:\src\ws-worldengine\feature\hcr\nimcache\@mmain.nim.cpp (312): main.exe!main()
d:\a01\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (79): main.exe!invoke_main()
d:\a01\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (288): main.exe!__scrt_common_main_seh() + 0x5 bytes
d:\a01\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (331): main.exe!__scrt_common_main()
d:\a01\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): main.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
ntdll.dll!RtlUserThreadStart() + 0x21 bytes
Data:
00 00 00 00 00 00 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak (61 bytes).
Largest number used: 61 bytes.
Total allocations: 61 bytes.
Visual Leak Detector is now exiting.
It looks like it's saying the memory allocated by nimNewObj is not being freed, but I can see a nimRawDispose being called in the generated C++. Is there something blocking these tools from detecting the free call done by --gc:arc.Is there something blocking these tools from detecting the free call done by --gc:arc?
Not that I know of but we allocate a single OutOfMem exception somewhere that is not freed.