Skip to content

Commit c1e34c5

Browse files
authored
Print memory leak info in Debug build of slangc.exe (shader-slang#4210)
When memory leak is detected, this commit will dump the information about the memory leak. This feature is available only in Debug build on Windows platform. Also note that the message will not be printed on the client applications that use slang.dll, because the printing happens as a part of slangc.exe not slang.dll. I found a bug that Slang::StdWriters was closing `stdout` and `stderr` in its destructor, which prevented Crt functions to print the messages to `stdout` and `stderr`.
1 parent f6b755d commit c1e34c5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

source/core/slang-std-writers.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Slang
1010
{
1111
RefPtr<StdWriters> stdWriters(new StdWriters);
1212

13-
RefPtr<FileWriter> stdError(new FileWriter(stderr, WriterFlag::AutoFlush));
14-
RefPtr<FileWriter> stdOut(new FileWriter(stdout, WriterFlag::AutoFlush));
13+
RefPtr<FileWriter> stdError(new FileWriter(stderr, WriterFlag::AutoFlush | WriterFlag::IsUnowned));
14+
RefPtr<FileWriter> stdOut(new FileWriter(stdout, WriterFlag::AutoFlush | WriterFlag::IsUnowned));
1515

1616
stdWriters->setWriter(SLANG_WRITER_CHANNEL_STD_ERROR, stdError);
1717
stdWriters->setWriter(SLANG_WRITER_CHANNEL_STD_OUTPUT, stdOut);

source/slangc/main.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ int wmain(int argc, wchar_t** argv)
139139
}
140140

141141
#ifdef _MSC_VER
142+
// "When _DEBUG isn't defined, calls to _CrtSetReportMode are removed
143+
// during preprocessing."
144+
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
145+
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
146+
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
147+
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
148+
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
149+
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
150+
142151
_CrtDumpMemoryLeaks();
143152
#endif
144153

0 commit comments

Comments
 (0)