Search code examples
c++memory-managementstdlist

Using delete on pointer to std::list?


In my program I have a pointer to a std::list object, it is allocated like so.

d_list_p = new std::list<some_type*>();

Then later in my program I delete it like so.

d_list_p->clear();
delete d_list_p;

For some reason I'm getting a Windows breakpoint triggered on the delete statement. If I break at the delete statement I see that the list exists and has a size of 0. Also, I never add an element to the list for the case that throws an error (I think).

The code is being compiled with the MS VC++ compiler for VS2005.

The error message says Windows triggered a breakpoint indicating memory corruption. The stack trace says the following.

ntdll.dll!DbgBreakPoint()   
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] 
ntdll.dll!RtlpNtMakeTemporaryKey()  + 0x6735 bytes  
ntdll.dll!RtlpNtMakeTemporaryKey()  + 0x6b72 bytes  
ntdll.dll!RtlpNtMakeTemporaryKey()  + 0x7d5a bytes  
ntdll.dll!LdrAlternateResourcesEnabled()  + 0x33bd bytes    
ntdll.dll!RtlpUnWaitCriticalSection()  + 0x65b bytes    
msvcr80.dll!free()  + 0xcd bytes    
FM_Access_Library_NET.dll!std::list<FM_Access_Library::Logger_Callbacks *,std::allocator<FM_Access_Library::Logger_Callbacks *> >::`scalar deleting destructor'()  + 0x20 bytes C++

It is probably worth mentioning that this delete statement is in C++ code that is being built into a .NET DLL, so the program is running in mixed-mode.


Solution

  • Is d_list_p a member of a class? And, does that class observe the Rule of Three?

    If not, then (a copy of) d_list_p may have already been deleted.