Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Free memory allocated in unmanaged DLL from C#

Free memory allocated in unmanaged DLL from C#

Scheduled Pinned Locked Moved C#
questioncsharpc++performancehelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Koushik Biswas
    wrote on last edited by
    #1

    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 DLL class 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!

    A 1 Reply Last reply
    0
    • K Koushik Biswas

      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 DLL class 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!

      A Offline
      A Offline
      albCode
      wrote on last edited by
      #2

      http://www.csharpfriends.com/Spec/index.aspx?specID=25.8.htm

      K 1 Reply Last reply
      0
      • A albCode

        http://www.csharpfriends.com/Spec/index.aspx?specID=25.8.htm

        K Offline
        K Offline
        Koushik Biswas
        wrote on last edited by
        #3

        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. No Marshal 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

        K 1 Reply Last reply
        0
        • K 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. No Marshal 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

          K Offline
          K Offline
          Koushik Biswas
          wrote on last edited by
          #4

          I mean, simply put, my question becomes: is there a way to call delete[] from C#? Koushik

          A G 2 Replies Last reply
          0
          • K Koushik Biswas

            I mean, simply put, my question becomes: is there a way to call delete[] from C#? Koushik

            A Offline
            A Offline
            albCode
            wrote on last edited by
            #5

            lookup this http://www.c-sharpcorner.com/Language/PointersInCSharpRVS.asp

            1 Reply Last reply
            0
            • K Koushik Biswas

              I mean, simply put, my question becomes: is there a way to call delete[] from C#? Koushik

              G Offline
              G Offline
              Gilad Kapelushnik
              wrote on last edited by
              #6

              I 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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups