Problem to delete unmanaged object

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: 1044

Expression: _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.)



Answer this question

Problem to delete unmanaged object

  • Sourav Parida

    Hi: when is the destructor being called From the information above it looks like the destructor is being called after the CRT has released the memory that was allocated for dynamic alllocations. I would use the debugger (press Retry/Recommencer) and take a look at the call-stack to see how the code gets to this point.

  • mikelaw

    No: you can't force the CRT to delete this pointer as it believes that the memory has already been freed. You are going to have to spend the time to find out why the CRT believes that the memory is not valid. I suspect that there is an ordering issue between the shutdown of the managed part of the process and the shutdown of the native portion. As I said before I suspect that the debug CRT heap facilities can be used to help track down what is going on here.

  • BoazJ

    This what's in the call stack :

    MSVCRTD.DLL!004636a8()
    [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

  • Cleber Dantas

    I'll try to find out but I wrote this thread because I'm tired of looking for a solution...

  • hazzoom

    I don't do see anything obviously wrong with this callstack. I would now use the debugger to track the value of the problematic pointer and make sure that it is not being overwritten by an invalid value or that it is not being accidentally being deleted twice.

  • Andrew Kidd

    With the debugger I see no other point where the variable is deleted. (The delete you see is the only one in my code and if I don't do it I get a memory leak)

  • AviF

    Unfortunately I can't see an easy solution: sorry.

  • Michaelxyz3

    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.



  • Cosmin Paun

    Thanks

  • Worachart

    But is there a solution to force deletion on this pointer

  • Problem to delete unmanaged object