how to dealloc memory allocated by COM server?

Hello,

(I accidentally posted this question previously in the Visual Studio General forum, when I was intending it for the Visual C# General forum. My apologies for the duplication.)

In my C# client, I am calling a COM server's method that allocates a buffer and returns the buffer and its length. It is the responsibility of the client to deallocate this buffer, but I do not know how to do so in C#.

The .NET signature looks like:

void IDeviceData.GetData (IntPtr responseBuffer, ref int responseLength);

The code looks (roughly) like this:

string s;
IDeviceData deviceIntf = Activator.CreateInstance(xxx);
unsafe
{
IntPtr resp = IntPtr.Zero;
IntPtr pResp = new IntPtr(&resp);
// server allocates buffer pointed to by <resp>, with length returned in <responseLength>
deviceIntf.GetData(pResp, ref responseLength);

if ((responseLength == 0) || (resp == IntPtr.Zero))
return;

s = Marshal.PtrToStringAnsi(resp, responseLength);

// TODO: need to dealloc resp
}

Any advice

Phil Coveney




Answer this question

how to dealloc memory allocated by COM server?

  • btravis

    PCoveney wrote:
    Is there a way to influence the signature created

    Not in this case but you can modify the generated interop assembly to make the signature look like you want. It's described here:

    http://msdn.microsoft.com/library/en-us/cpguide/html/cpconeditinginteropassembly.asp

    PCoveney wrote:
    Or, does making it an out IntPtr mean that the GC will deallocate it when the actual parameter goes out of scope

    No you'll have to do that manually.



  • Raichur

    AFAIK it is not possible to directly deallocate memory allocated within another process (e.g., your COM server). Normally in this kind of situation the owner of the memory (the COM server) also provides a method that you call to get the owner to deallocate the memory, or it deallocates it automatically at some point.

    Alternatively, is it the caller who allocates the memory, and passes the buffer and its length to the callee. Are you sure this is not what you're expected to do

    It is good practice for the owner of allocated memory to be responsible for deallocating it.


  • Manu Srikumar

    Mattias,

    Thanks, the link was helpful. I have used the information I found there to change the signature as you suggested in your earlier post.

    void GetData (out IntPtr responseBuffer, ref int responseLength);

    But, I do not understand how this change influences my ability to deallocate the memory. In an earlier post, you said

    >>How you free the memory depends on how the callee allocates it.

    I am not sure, but I think you may have been referring to the various Marshal methods that free memory. The data is not a BSTR, so I can't use FreeBSTR(). The server is unmanaged, which I am using in-process, so I believe the pointer is one resulting from malloc() or new, and just represents a block on the process heap. This lets out FreeCoTaskMem() and FreeHGlobal().

    Or, did you mean something else If so, what

    Thanks (again),

    Phil Coveney



  • Naveen koul

    You could get rid of the need for unsafe code if you change the signature to

    void GetData (out IntPtr responseBuffer, ref int responseLength);

    How you free the memory depends on how the callee allocates it.



  • bakuls

    I assume it's an in-proc COM server. Otherwise it would be pointless to return a raw pointer to the caller.



  • Dark_V

    Mattias,

    Thank you for this suggestion, but I do not know how to accomplish it. In my C# client, I have added a reference to the COM server that provides this interface. I am using the signature provided by Interop. Is there a way to influence the signature created

    By the way, I shouldd have mentioned in my earlier post...the COM signature in the IDL file looks like

    HRESULT GetData ([out] byte** ppData, [in, out] *pLen);

    But, even if this change is made, it still does not allow me to deallocate the buffer, right Or, does making it an out IntPtr mean that the GC will deallocate it when the actual parameter goes out of scope

    Thanks for your help,

    Phil Coveney



  • anchorpoint

    If the memory was allocated with malloc() or new it has to be released with free() or delete in the same runtime library. That's why it's generally considered bad practice to return such pointers without also providing a corresponding method to release the memory. It's better to use a language neutral allocation API provided by the platform.



  • how to dealloc memory allocated by COM server?