Free memory allocated in unmanaged DLL from C#
-
I will ask the question with a 2 step introduction... Please help me. Step 1: I write an unmanaged DLL in C++. Here is the exported function:
SAMPLEDLL_API void StringAsOUT_AllocatedInDll( char ** szParam ) { *szParam = new char [ 128 ]; strcpy( *szParam, "Please free my memory after using me!" ); }
Step 2: I write a C# managed EXE to use this DLLclass Imports { [DllImport("SampleDLL.dll")] public static extern void StringAsOUT_AllocatedInDll( ref int szParam ); ... }
... Imports impObj = new Imports(); int iAddressOfANSIString = 0; impObj.StringAsOUT_AllocatedInDll( ref iAddressOfANSIString ); string strOUTPUTFromUnmanagedDll = Marshal.PtrToStringAnsi( ( IntPtr ) iAddressOfANSIString ); Console.WriteLine( "Value of strOUTPUTFromUnmanagedDll = {0}", strOUTPUTFromUnmanagedDll ); ...
Question: The code works. It does give console output "Value of strOUTPUTFromUnmanagedDll = Please free my memory after using me!" But isn't there a memory leak? How do I free the memory allocated by new[] inside the unmanaged DLL? All the Marshal class helps artciles always refer to how to free an unmanaged memory block when it is allocated from .NET code. But here it is allocated by unamanaged code. If anybody can please help me with this, I will be grateful. Thank you! -
I will ask the question with a 2 step introduction... Please help me. Step 1: I write an unmanaged DLL in C++. Here is the exported function:
SAMPLEDLL_API void StringAsOUT_AllocatedInDll( char ** szParam ) { *szParam = new char [ 128 ]; strcpy( *szParam, "Please free my memory after using me!" ); }
Step 2: I write a C# managed EXE to use this DLLclass Imports { [DllImport("SampleDLL.dll")] public static extern void StringAsOUT_AllocatedInDll( ref int szParam ); ... }
... Imports impObj = new Imports(); int iAddressOfANSIString = 0; impObj.StringAsOUT_AllocatedInDll( ref iAddressOfANSIString ); string strOUTPUTFromUnmanagedDll = Marshal.PtrToStringAnsi( ( IntPtr ) iAddressOfANSIString ); Console.WriteLine( "Value of strOUTPUTFromUnmanagedDll = {0}", strOUTPUTFromUnmanagedDll ); ...
Question: The code works. It does give console output "Value of strOUTPUTFromUnmanagedDll = Please free my memory after using me!" But isn't there a memory leak? How do I free the memory allocated by new[] inside the unmanaged DLL? All the Marshal class helps artciles always refer to how to free an unmanaged memory block when it is allocated from .NET code. But here it is allocated by unamanaged code. If anybody can please help me with this, I will be grateful. Thank you! -
Thanks Albanian. But that really does not answer my question - as you see the article describes how to free the memory only if it was allocated from the C# code. Like this: You in C#: you allocate some memory in unmanaged heap, and then you free it anfter using it. That is what the article shows. But my situation is different: I am calling an unmanaged DLL that allocates the memory and returns the pointer. I use it from C#. There is no corresponding call back to the unmanaged DLL that will free the memory. I will have to free it myself from C# after using it. As I did not allocate it, I don't have the
HGlobal
Handle or anything - all I have is the address returned. NoMarshal
class function is helping me out! Thanks again for seeing my post and replying - I have been waiting for 2 days (i had posted this 2 days ago also) - but nobosy seems to have an answer!:sigh::sigh::sigh::sigh: Koushik Biswas -
Thanks Albanian. But that really does not answer my question - as you see the article describes how to free the memory only if it was allocated from the C# code. Like this: You in C#: you allocate some memory in unmanaged heap, and then you free it anfter using it. That is what the article shows. But my situation is different: I am calling an unmanaged DLL that allocates the memory and returns the pointer. I use it from C#. There is no corresponding call back to the unmanaged DLL that will free the memory. I will have to free it myself from C# after using it. As I did not allocate it, I don't have the
HGlobal
Handle or anything - all I have is the address returned. NoMarshal
class function is helping me out! Thanks again for seeing my post and replying - I have been waiting for 2 days (i had posted this 2 days ago also) - but nobosy seems to have an answer!:sigh::sigh::sigh::sigh: Koushik BiswasI mean, simply put, my question becomes: is there a way to call
delete[]
from C#? Koushik -
I mean, simply put, my question becomes: is there a way to call
delete[]
from C#? Koushik -
I mean, simply put, my question becomes: is there a way to call
delete[]
from C#? KoushikI was trying to do the same for some time. Eventualy I realized that the best method is to allocate memory in C# and send a referance of the memory array to the C++ mthod\function. You might have a probelm determining the size of the array needed but that is a different story. Even if you get it to work, it would be very careless doing so. Gilad.