Hello,
I would like to know why I'm unable to delete an unmanaged object :
Here is the code :
public ref class SpeGui : public System::Windows::Forms::Form
{
private:
String^ m_sCompassPort;
int m_nCompassBaudRate;
int m_nCompassDataBits;
int m_nCompassStopBits;
int m_nCompassParity;
int m_nCompassRefreshPeriod;
int m_nCompassAccuracy;
PI_DigitalCompass * m_pThCompass;
MyCompassObserver * m_pMyCompassObserver;
...
}
SpeGui::SpeGui()
{
...
InitializeComponent();
...
//Cast de String^ vers const char*
IntPtr strptr = (Marshal::StringToHGlobalAnsi(m_sCompassPort));
const char* port = (const char*)strptr.ToPointer();m_pThCompass =
new PI_DigitalCompass(port,
m_nCompassBaudRate,
m_nCompassDataBits,
m_nCompassStopBits,
m_nCompassParity,
m_nCompassRefreshPeriod,
m_nCompassAccuracy
);
m_pMyCompassObserver =
new MyCompassObserver();...
Marshal::FreeHGlobal(strptr);startCompass();
port = NULL;}
SpeGui::~SpeGui(){
try{
stopCompass();
delete m_pThCompass;m_pThCompass = NULL;
delete m_pMyCompassObserver;m_pMyCompassObserver = NULL;
...
}
catch(Exception^ e){
log(
"[~SpeGui] Exception : " + "Source : \n" + e->Source + "\nMessage : \n" + e->Message);}
...}
I get no problem to delete m_pMyCompassObserver, that is unmanaged, still I get this message when I'm trying to delete m_pThCompass :
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!Program: machin.exe
File: dbgheap.c
Line: 1044Expression: _CrtIsValidHeapPointer(pUserData)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.(Press Retry to debug the application)
---------------------------
Abandonner Recommencer Ignorer
---------------------------
In fact the PI_DigitalCompass class is imported with a .h interface plus a .lib library. How can I escape this problem, using these two files (If I don't delete m_pThCompass I get a memory leak of course. I checked up and I'm sure the destructor of PI_DigitalCompass has been well implemented.)

Problem to delete unmanaged object
Martin Dietz
Julien Gourdet SOPRA
You should take a look at using the debug CRT heap:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/vsdebug/html/_core_solving_buffer_overwrites_and_memory_leaks.asp
This should help you track down what is going on here.
KeWin
Artur I.
Whatfly
Steini
tculler
Wesley_Case
ruben_ruvalcaba_camba
Yhack
[Les frames ci-dessous sont peut-etre incorrects et/ou manquants, aucun symbole charge pour MSVCRTD.DLL]
MSVCRTD.DLL!004635b1()
MSVCRTD.DLL!0046297d()
PI_Compass.dll!1000197a()
[Transition Manage a Natif]
> NFPE.exe!NavStreamPilotEmbedded::SpeGui::~SpeGui() Ligne 712 + 0x33 octets C++
NFPE.exe!NavStreamPilotEmbedded.SpeGui.Dispose + 0x32 octets
System.dll!System.ComponentModel.Component.Dispose + 0xf octets
System.Windows.Forms.dll!System.Windows.Forms.Form.WmClose + 0x156 octets
System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc + 0x16d octets
NFPE.exe!NavStreamPilotEmbedded::SpeGui::WndProc(System::Windows::Forms::Message m = 0x0012e090) Ligne 86 C++
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage + 0xd octets
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc + 0xd6 octets
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback + 0x75 octets
[Transition Natif a Manage]
user32.dll!77d18734()
user32.dll!77d18816()
user32.dll!77d1b89b()
user32.dll!77d1b903()
[Transition Manage a Natif]
System.Windows.Forms.dll!System.Windows.Forms.Control.SendMessage + 0x3d octets
System.Windows.Forms.dll!System.Windows.Forms.Form.Close + 0x61 octets