Freeing Memory allocted in unmanged C code

S

skg

I am passing the address of pointer like char** from managed extension and
getting the
its initialized value from a C library dll.

How can i free the memory from the code in Managed Extension ?

Here is a sample C function i am calling from managed extension.

C dll Code
==============

extern "C" void __declspec(dllexport) Test(char **p)
{

int nSize = 10000;

*p= (char*)::malloc(nSize);

while(--nSize)

*(*p+nSize)='A';



}

// Iam currently calling back to C dll to free the memory it allocated.

extern "C" void __declspec(dllexport) TestFree(char **p)

{

::free(*p);

}

Managed Extension Code Function.
====================
StringBuilder *strB;

char *pszString;

Test(&pszString);

strB->Append(pszString);

//Marshal::FreeCoTaskMem((IntPtr)pszString); //This throws exception.after
certain number of iterations of calling this function

TestFree(&pszString); // I am currently calling this since above statement
does not work.

How can i free the memory in Managed Extension ?
Thanks!!!!
 
W

Willy Denoyette [MVP]

Memory allocated using malloc() must be freed by calling free() as you did.
Memory allocated using CoTaskMemAlloc must be freed using FreeCoTaskMem.


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top