Observing memory leak on Windows Server 2016 with a 32-bit C++ application

Sanjeev Gupta 1 Reputation point
2021-09-08T04:40:31.233+00:00

Hello,

We have a win32 C++ application. We started observing memory leak issue with it when running this application on Windows Server 2016.

We have a hierarchy of exception classes and the base exception class handles the stack trace generation and string conversion for logging.
• When the exceptions are thrown frequently, we see that with the increase in number of exceptions thrown and time, the memory consumption of the service that throws these exceptions increases when the application is running on Windows Server 2016.
E.g. if we keep the service running for 2+ days (regularly throwing exceptions) the consumption reaches 190 MB and continues to increase

• We have traced and listed down the Microsoft APIs that are called when this exception is thrown:

  1. _wgetenv()
  2. GetCurrentThread()
  3. GetCurrentProcess()
  4. RtlCaptureContext( &ctxt )
  5. StackWalk64( IMAGE_FILE_MACHINE_I386, process, thread, &frame, &ctxt, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL )
  6. SymSetOptions( SYMOPT_CASE_INSENSITIVE | SYMOPT_LOAD_LINES )
  7. GetModuleFileNameA()
  8. SymInitialize()
  9. SymGetSymFromAddr()
  10. SymGetLineFromAddr()
  11. EnumProcessModules()
  12. GetModuleInformation()
  13. GetModuleFileNameA()
  14. GetFileAttributesA()
  15. SymLoadModule64()
  16. GetModuleFileNameW()
  17. SymCleanup(process)

• And below Structures are used:

  1. ::STACKFRAME64 frame
  2. ::CONTEXT ctxt

• All these APIs are called from a DLL whose methods are invoked when the exception is thrown by the service
• The base exception class definition is also present in the DLL code, whereas the actual derived exception class is defined in the exe code.

We have checked and observed below –
• The issue does not occur on 2012 and 2019 server; hence, the issue is specific to windows server 2016
• We also tried to check if there is any difference between 2012 and 2016 server in terms of allocation and deallocation of resources by these APIs on Microsoft’s official documentation but could not find anything conclusive

Please let me know if more details are required.
Thanks in advance,

Sanjeev

Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,389 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,391 Reputation points
    2021-09-08T18:27:30.227+00:00

    Hello @Sanjeev Gupta

    I would like to suggest you to check below Microsoft article discussing the same however its for Windows 7 and 2008 R2 but may help in your situation.
    https://learn.microsoft.com/en-us/windows/win32/win7appqual/preventing-memory-leaks-in-windows-applications

    If the reply was helpful, please don’t forget to upvote or accept as answer.