0

Is there a way to profile memory allocated in an external DLL, in my case the SysAllocString function?

The DLL is compiled from Delphi and I have control over both the Delphi and C# code if instrumentation needs to be written.

In the overview of the Visual Studio memory profiler it says the Windows SDK has been annotated to emit ETW event data which is tracked by the profiler. However I've been unable to confidently confirm if the DLL's SysAllocString memory is tracked in it.

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
JoBe
  • 33
  • 6
  • If you are calling a dll from c# using DllImport the memory has to be allocated either using the Windows Allocate method or in c# (not c++). The problem is calling a method the local memory is placed on the execution stack. When you return from the method the stack is no longer valid so you can't extract the data after the return. So you have to allocate in c# in non managed memory space using IntPtr ptr = Marshal.AllocHGlobal(size); – jdweng Apr 13 '20 at 17:59
  • @jdweng In the DLL the memory is allocated with the SysAllocString function from the windows API. In the C# it is accessible and fine to use, so the marshalling is working fine. My question is more about what happens to the memory allocated by SysAllocString in the Delphi code, and if there is a way to profile the memory allocated by the DLL. Hope that's clearer. – JoBe Apr 14 '20 at 06:33
  • In c# you use Marshal.AllocCoTaskMem(size); and Marshal.FreeCoTaskMem(IntPtr); So you can free the memory in c# when you are done with the memory. Or you can call a method in the DLL to free the memory. – jdweng Apr 14 '20 at 06:57
  • @jdweng You're misunderstanding the question. I know I can free the memory on the C# side, what I'm looking for is information regarding memory profiling a C# application which includes the memory allocated by an external unmanaged DLL. To find potential leaks. – JoBe Apr 14 '20 at 14:17
  • I'm not familiar with any of the tools that does memory profiling. – jdweng Apr 14 '20 at 15:37

0 Answers0