
Destructors/Singleton Pattern
Hi,
I am getting an Unhandled exception error due to an access violation. I
basically have a singleton object which two other objects access via
their respective pointers. I am having problems cleaning up all my
objects.
When the f_Categorizer destructor is invoked, I want to delete the
pointer, f_exitEvent to the singleton object, f_exit_categorize_event.
The pointer f_exitEvent should be gone. Deleting this pointer invokes
the f_exit_categorize_event destructor. In here, I have a check to make
sure that we will only delete the _instance pointer, which points to the
f_exit_categorize_event object, if there are no other objects that use
it . In this case count=1, since Emulator is still pointing to it, so
nothing should happen to _instance.
Now when ~Emulator is called, it too wants to delete its pointer to the
f_exit_categorize_event object. I am getting the error at this point. My
object should still be allocated since I have not destroyed the last
pointer to it, right?
My destructor code is attached and the disassembly is attached also. Can
anyone see what I am doing wrong?
Thanks ahead of time....gary
The destructor for the singleton object is:
f_exit_categorize_event::~f_exit_categorize_event()
{
if (--count == 0)
{
delete _instance;
}
f_Categorizer::~f_Categorizer()
{
delete f_exitEvent; // f_exitEvent =
f_exit_categorize_event::instance(); => instance returns a pointer to
the f_exit_categorize_event object
Quote:
}
Emulator::~Emulator()
{
delete Forward_CategorizeExit; // Forward_CategorizeExit =
f_exit_categorize_event::instance();
// =>
instance returns a pointer to the f_exit_categorize_event object
Quote:
}
I don't know how to interpret the disassembly instructions which vc
provides. Can anyone point out what it means or a reference where I can
interpret the machine instructions?
82: delete Forward_CategorizeExit;
10003ACA mov eax,dword ptr [ebp-10h]
10003ACD mov ecx,dword ptr [eax+80h]
10003AD3 mov dword ptr [ebp-2Ch],ecx
10003AD6 mov edx,dword ptr [ebp-2Ch]
10003AD9 mov dword ptr [ebp-28h],edx
10003ADC cmp dword ptr [ebp-28h],0
10003AE0 je Emulator::~Emulator+0E3h (10003af3)
10003AE2 push 1
10003AE4 mov eax,dword ptr [ebp-28h]
10003AE7 mov edx,dword ptr [eax]
10003AE9 mov ecx,dword ptr [ebp-28h]
===> 10003AEC call dword ptr [edx] <===== code raises error
here
10003AEE mov dword ptr [ebp-50h],eax
10003AF1 jmp Emulator::~Emulator+0EAh (10003afa)
10003AF3 mov dword ptr [ebp-50h],0