|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Unhandled exceptionfunction 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!!! microsoft.public.dotnet.languages.vc is a better group for C++
questions. >I have an application using managed extension for C++ which calls a Does the InitializeNames function takes a char*& parameter? (I can't>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 ; 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); I'm not sure where pAttNamesList came from so I'm not sure what this> >System::IntPtr target = Marshal::ReadIntPtr(pAttNamesList); > >Marshal::FreeCoTaskMem(target); //Frees any kind of allcated memory 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. 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. >How can i free the memory in Managed Extension ? Just make sure you use the same family of memory allocation APIs onboth 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.
Other interesting topics
Anonymous type etc. in C# 3.0
implicit types in C# 3.0, what is the usefulness of them? ArrayList, can't add ? Generic Generics Problem 2 numbers aren't sorting properly Security Exception? Getting args array from files launch through file association How to change size of PictureBox from code ? Newbie problem with button click event arguments RightToLeft tabControl |
|||||||||||||||||||||||