473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unhandled exception

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


Nov 17 '05 #1
3 1537

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.
Nov 17 '05 #2
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!!!!

"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.

Nov 17 '05 #3
>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.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote...
5
by: Dave Stewart | last post by:
I recently wrote my first Vb.net application, or at least my first complex app since moving up from vb6. When run from the VS.NET IDE, the program shows no errors and runs fine. When the output exe...
5
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by...
5
by: Samuel R. Neff | last post by:
When you have an unhandled exception in vb.net how do you view the exception information in the debugger? In C# the debugger creates a local variable that points to the exception and you can...
0
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks...
5
by: Simon Tamman {Uchiha Jax} | last post by:
Now this is bugging me. I just released software for a client and they have reported an unhandled stack overflow exception. My first concern is that the entirity of the UI and any threaded...
0
by: Bob | last post by:
If I induce and unhandled exception in my vb.net code for instance using '-- just a simple "object not initialized" exception (should read "as new") Dim x As Specialized.NameValueCollection ...
1
by: Bob | last post by:
In Vs 2005 you have new applicationsEvents.vb I was testing it in a simple app and found that it was easier to implement unhandled exception management tah it was in Vs2003 (vb.net) You can, if you...
4
by: bg_ie | last post by:
Hi, I have the following Program.cs - namespace TestFrameworkApplication { static class Program { /// <summary> /// The main entry point for the application.
5
by: =?Utf-8?B?c3VydHVyeg==?= | last post by:
Hi, I feel like a noob for asking this. When I publish a VB windows application, I want to disable the ability of the the user to continue when there is an unhandled exception. For example,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.