C++ utility calling "delete this" fails with exception on 2008.
I've a 32-bit command line C++ utility that fails on 2008 server. Code
compiled with VS2005. The utility works fine (and released) on windows 2003
(64-bit too). I don't have any sample of reasonable size, but here is the
description.
1. Class Hierarchy in DLL
MyObjectBase{
AddRef() { Count++; return; }
Release() { if (Count <= 0) delete this; return;}
class MyIntClass : public MyObjectBase { }; // There are other classes too
in the hierarchy.
class MyCMGetIPListMessage : public MyObjectBase { }
This heirarchy is implemented in MyBase.DLL and MyCMMessage.DLL
2. C++ Utility
GetNums.exe uses SmartPointer Template class which instantiate classes from
the above hierarchy, mainly leaf classes.
Template <class T> class SmartPointer { T* m_pObject;
SmartPointer() {m_pObject->AddRef();}
~SmartPointer() {m_pObject->Release();}
This way when SmartPointer goes out of scope
the classes on the heap get deleted. Code in GetNums.cpp is like this.
int main() {
SmartPointer<MyCMGetIPListMessage> spMsg = new MyCMGetIPListMessage;
return 0; // Causes exception; See call stack below
// exit (0); // No exception because ~() are not called.
}
/****
ChildEBP RetAddr
0017fd0c 77dac7b8 ntdll!DbgBreakPoint
0017fd14 77d67767 ntdll!RtlpBreakPointHeap+0x28
0017fd30 77d45275 ntdll!RtlpValidateHeapEntry+0x16d
0017fd78 77d45180 ntdll!RtlDebugFreeHeap+0x9a
0017fe6c 77d2f285 ntdll!RtlpFreeHeap+0x5f
0017fe88 76ca3593 ntdll!RtlFreeHeap+0x14e
0017fe9c 74f14c39 kernel32!HeapFree+0x14
0017fee8 001a5ac8 MSVCR80!free+0xcd
0017fef8 1c00717a MyCMMessage!MyCMGetIPListMessage::`vector deleting
destructor'+0x48
0017ff28 0040208b MyBase!MyObjectBase::Release+0x8a
0017ff44 00403ad1 GetNums!main+0xcb
0017ff88 76cae3f3 GetNums!__tmainCRTStartup+0x10f
[f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c @ 597]
0017ff94 77d7cfed kernel32!BaseThreadInitThunk+0xe
0017ffd4 77d7d1ff ntdll!__RtlUserThreadStart+0x23
0017ffec 00000000 ntdll!_RtlUserThreadStart+0x1b
*****/
Any help from experts out there.