Home All Groups Group Topic Archive Search About
Author
14 Sep 2005 7:53 PM
skg
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 ;

String* szFileList = Marshal::PtrToStringAnsi(target);

System::IntPtr target = Marshal::ReadIntPtr(pAttNamesList);

Marshal::FreeCoTaskMem(target); //Frees any kind of allcated memory


After running the above code in a loop for 20-30 times I get following
error.
"Unhandled exception at 0x7c901230" and debugger goes to dbgheap.c

can any one suggest what iam doing wrong.
Thanks!!!

Author
14 Sep 2005 8:36 PM
Mattias Sjögren
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.
Are all your drivers up to date? click for free checkup

Author
15 Sep 2005 2:35 PM
skg
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!!!!



Show quoteHide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:%23C7ZSvWuFHA.2880@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.
Author
16 Sep 2005 8:14 PM
Mattias Sjögren
>How can i free the memory in Managed Extension ?

Just make sure you use the same family of memory allocation APIs on
both sides. For example, allocate the memory with CoTaskMemAlloc (or
Marshal::AllocCoTaskMem) if you want to free it with
Marshal::FreeCoTaskMem on the managed side.


Mattias

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Bookmark and Share