Unhandled exception

S

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!!!
 
M

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
 
S

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!!!!
 
M

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
 

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