HighTechTalks DotNet Forums  

How to free the memory allocated in unmanged code from managed code?

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss How to free the memory allocated in unmanged code from managed code? in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
m...
 
Posts: n/a

Default How to free the memory allocated in unmanged code from managed code? - 07-17-2007 , 08:49 AM






Hi,

I am using a COM component which provides a function like
GetBitmapData(out BYTE* pData, out long *pLen), in which it allocates
the memory using "new" and returns the pointer and length of memory
block.

i am calling this functon in C# in following way...
int pImageBuffer = 0;
int len = 0;

m_COMAdapter.GetBitmapData(ref pImageBuffer, out len);
IntPtr ImageData = new IntPtr(pImageBuffer);
//I use IntPtr
//Now I need to free up the memory allocated by GetBitmapData()

I am not sure how i can free up the memory here?

I tried Marshal.FreeCoTaskMem(ImageData), but its not helping and i am
sure if it is the correct way...

thx,
m...


Reply With Quote
  #2  
Old   
Micky Duncan
 
Posts: n/a

Default Re: How to free the memory allocated in unmanged code from managed code? - 07-29-2007 , 10:50 PM






Hi,
If your COM object allocates the data then it should be the one that frees
it too. Add a method called FreeBitmapData() or something on the COM object.

This is actually a good practice from the days of c++ DLLs where memory is
allocated and shared amonst the DLL's clients. Clients don't free the data
but rather the one who allocated it in the first place - the DLL

Cheers,
Micky

"m..." <plane.newpost (AT) gmail (DOT) com> wrote

Quote:
Hi,

I am using a COM component which provides a function like
GetBitmapData(out BYTE* pData, out long *pLen), in which it allocates
the memory using "new" and returns the pointer and length of memory
block.

i am calling this functon in C# in following way...
int pImageBuffer = 0;
int len = 0;

m_COMAdapter.GetBitmapData(ref pImageBuffer, out len);
IntPtr ImageData = new IntPtr(pImageBuffer);
//I use IntPtr
//Now I need to free up the memory allocated by GetBitmapData()

I am not sure how i can free up the memory here?

I tried Marshal.FreeCoTaskMem(ImageData), but its not helping and i am
sure if it is the correct way...

thx,
m...



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.