shader-slang / slang

Making it easier to work with shaders
MIT License
1.78k stars 158 forks source link

Fix memory leak in slang.dll #4208

Open jkwak-work opened 1 month ago

jkwak-work commented 1 month ago

When we run "slangc.exe", _CrtDumpMemoryLeaks() reports the memory leaks on Windows platform. The memory leak has been reported for a while but it has been ignored. It is because the messages are printed to the "debug output", which is visible only if you ran slangc.exe from VisualStudio.

Memory leak is never a good thing and we should fix it. We should also setup CI to prevent it from happening in the first place.

jkwak-work commented 1 month ago

When the change #4210 is merged, the issue can be reproduced by just running a Debug build slangc.exe; not Release. Here is an example,

$ build/Debug/bin/slangc.exe
Detected memory leaks!
Dumping objects ->
{1319712} normal block at 0x0000021ACE6BE010, 48 bytes long.
 Data: <`\&             > 60 5C 26 C4 1A 02 00 00 00 00 00 00 00 00 00 00
{3949} normal block at 0x0000021AC4265C60, 48 bytes long.
 Data: <          k     > 00 00 00 00 00 00 00 00 10 E0 6B CE 1A 02 00 00
{3948} normal block at 0x0000021AC425F420, 32 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00
{3947} normal block at 0x0000021AC4201C50, 1024 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
{3921} normal block at 0x0000021AC420B4C0, 904 bytes long.
 Data: <X a             > 58 D1 61 F5 FA 7F 00 00 01 00 00 00 00 00 00 00
{234} normal block at 0x0000021AC41DBED0, 1024 bytes long.
 Data: < 7              > 80 37 92 F5 FA 7F 00 00 D4 A6 A7 F5 FA 7F 00 00
{215} normal block at 0x0000021AC41DBA90, 1024 bytes long.
 Data: <                > 10 AC 91 F5 FA 7F 00 00 C0 BE A7 F5 FA 7F 00 00
{200} normal block at 0x0000021AC41DB650, 1024 bytes long.
 Data: <                > FC A6 91 F5 FA 7F 00 00 B4 A6 A7 F5 FA 7F 00 00
{190} normal block at 0x0000021AC41DB210, 1024 bytes long.
 Data: <                > E0 A1 91 F5 FA 7F 00 00 D0 A6 A7 F5 FA 7F 00 00
{169} normal block at 0x0000021AC41DADD0, 1024 bytes long.
 Data: <                > 90 9B 91 F5 FA 7F 00 00 B4 A6 A7 F5 FA 7F 00 00
{168} normal block at 0x0000021AC41DAB80, 528 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 90 AB 1D C4 1A 02 00 00
{163} normal block at 0x0000021AC41BF030, 128 bytes long.
 Data: <                > 00 00 00 00 00 00 00 FC FF 00 00 00 00 00 00 00
{161} normal block at 0x0000021AC41BF7B0, 128 bytes long.
 Data: <                > FC 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.

The message above shows 13 objects were still using memory when slangc.exe was about to exit.

sriramm-nv commented 4 weeks ago

I see a lot more than this

Detected memory leaks!
Dumping objects ->
{1355939} normal block at 0x000001B7A5050650, 48 bytes long.
 Data: <                > 10 FA 04 A5 B7 01 00 00 00 00 00 00 00 00 00 00
{1355631} normal block at 0x000001B7A504FA10, 48 bytes long.
 Data: <p       P       > 70 FE 04 A5 B7 01 00 00 50 06 05 A5 B7 01 00 00
{1355459} normal block at 0x000001B7A504FE70, 48 bytes long.
 Data: <                > B0 03 05 A5 B7 01 00 00 10 FA 04 A5 B7 01 00 00
{1355203} normal block at 0x000001B7A50503B0, 48 bytes long.
 Data: <        p       > E0 05 05 A5 B7 01 00 00 70 FE 04 A5 B7 01 00 00
{1353519} normal block at 0x000001B7A50505E0, 48 bytes long.
 Data: <                > 20 FD 04 A5 B7 01 00 00 B0 03 05 A5 B7 01 00 00
{1353456} normal block at 0x000001B7A504FD20, 48 bytes long.
 Data: <@               > 40 0A 05 A5 B7 01 00 00 E0 05 05 A5 B7 01 00 00
{1353263} normal block at 0x000001B7A5050A40, 48 bytes long.
 Data: <                > A0 F9 04 A5 B7 01 00 00 20 FD 04 A5 B7 01 00 00
{1352320} normal block at 0x000001B7A504F9A0, 48 bytes long.
 Data: <        @       > 10 A6 04 A5 B7 01 00 00 40 0A 05 A5 B7 01 00 00
{1339283} normal block at 0x000001B7A504A610, 48 bytes long.
 Data: <                > B0 A8 04 A5 B7 01 00 00 A0 F9 04 A5 B7 01 00 00
{1339282} normal block at 0x000001B7A504A8B0, 48 bytes long.
 Data: <                > A0 A5 04 A5 B7 01 00 00 10 A6 04 A5 B7 01 00 00
{1338916} normal block at 0x000001B7A504A5A0, 48 bytes long.
 Data: <P               > 50 AB 04 A5 B7 01 00 00 B0 A8 04 A5 B7 01 00 00
{1338704} normal block at 0x000001B7A504AB50, 48 bytes long.
 Data: < q              > 20 71 04 A5 B7 01 00 00 A0 A5 04 A5 B7 01 00 00
{1322944} normal block at 0x000001B7A5047120, 48 bytes long.
 Data: <0P      P       > 30 50 D4 9A B7 01 00 00 50 AB 04 A5 B7 01 00 00
{3986} normal block at 0x000001B79AD45030, 48 bytes long.
 Data: <         q      > 00 00 00 00 00 00 00 00 20 71 04 A5 B7 01 00 00
{3985} normal block at 0x000001B79AD41AD0, 32 bytes long.
 Data: <         A      > 00 00 01 04 01 00 00 00 14 41 04 00 10 04 10 00
{3984} normal block at 0x000001B79ACD5360, 1024 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
{3961} normal block at 0x000001B79ACC8A20, 904 bytes long.
 Data: <X /L            > 58 F9 2F 4C FF 7F 00 00 01 00 00 00 00 00 00 00
{241} normal block at 0x000001B79ACB9510, 1024 bytes long.
 Data: < /aL    $ vL    > 18 2F 61 4C FF 7F 00 00 24 86 76 4C FF 7F 00 00
{222} normal block at 0x000001B79ACB90D0, 1024 bytes long.
 Data: <  `L      vL    > 20 A4 60 4C FF 7F 00 00 10 9E 76 4C FF 7F 00 00
{207} normal block at 0x000001B79ACB8C90, 1024 bytes long.
 Data: <  `L      vL    > F4 9E 60 4C FF 7F 00 00 04 86 76 4C FF 7F 00 00
{197} normal block at 0x000001B79ACB8850, 1024 bytes long.
 Data: <  `L      vL    > C8 99 60 4C FF 7F 00 00 20 86 76 4C FF 7F 00 00
{176} normal block at 0x000001B79ACB8410, 1024 bytes long.
 Data: <  `L      vL    > 90 93 60 4C FF 7F 00 00 04 86 76 4C FF 7F 00 00
{175} normal block at 0x000001B79ACB81C0, 528 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 D0 81 CB 9A B7 01 00 00
{170} normal block at 0x000001B79AC9F0D0, 128 bytes long.
 Data: <                > 00 00 00 00 00 00 00 FC FF 00 00 00 00 00 00 00
{167} normal block at 0x000001B79AC9E950, 128 bytes long.
 Data: <                > FC 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
csyonghe commented 3 weeks ago

We can use _crtBreakAlloc to track down where those allocations are made. I suspsect they may just be the global dictionary used in our profiler.