Mattias, Thanks!!!
Yes you are right. I am passing the address of pointer and getting the
initialized value.
Here is a sample C function iam 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.
====================
StringBuilder *strB;
char *pszString;
Test(&pszString);
strB->Append(pszString);
//Marshal::FreeCoTaskMem((IntPtr)pszString); //This throws exception.
TestFree(&pszString); // I am currently calling this since above statement
does not work.
How can i free the memory in Managed Extension ?
Thanks!!!!
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
microsoft.public.dotnet.languages.vc is a better group for C++
questions.
I have an application using managed extension for C++ which calls a
function in a dll written in C.
That function initializes a character pointer passed to it..
char *pfNamesList = NULL;
InitializeNames(pfNamesList ); // Allocates memory using malloc and
initilaizes the pointer to it.
System::IntPtr target = pfNamesList ;
Does the InitializeNames function takes a char*& parameter? (I can't
tell without seeing the function declaration). If not, there's no way
the function can modify the pfNamesList variable since it's passed by
value.
String* szFileList = Marshal::PtrToStringAnsi(target);
System::IntPtr target = Marshal::ReadIntPtr(pAttNamesList);
Marshal::FreeCoTaskMem(target); //Frees any kind of allcated memory
I'm not sure where pAttNamesList came from so I'm not sure what this
code does. But unless you're incrementing pAttNamesList for each loop
iteration, you will be retrieving and freeing the same "target" value
every time. And freeing the same memory multiple times could well
cause the exception you're getting.
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.