Destructors/Singleton Pattern 
Author Message
 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



Sat, 02 Nov 2002 03:00:00 GMT  
 Destructors/Singleton Pattern

Hi,
the problem is that delete not only calls the destructor of your singleton.
Delete deallocates also the dynamic memory for
f_exit_categorize_event::_instance. After you first deleted the object the
reference (_instance) points to an perfect invalid object.

chungary schrieb:

Quote:
> 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;

would mean:
delete this; // !!!

- Show quoted text -

Quote:

>  }

> 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
> }

> Emulator::~Emulator()
> {
>  delete Forward_CategorizeExit;        // Forward_CategorizeExit =
> f_exit_categorize_event::instance();
>                                                          //    =>
> instance returns a pointer to the f_exit_categorize_event object
> }

> 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

gets the addresse of the destructor throuw the virtuel function table and
call the destructor. After the destructor of an object was called i think it
overwrites it with nonsens to mark it invaild.

Quote:
> here
> 10003AEE   mov         dword ptr [ebp-50h],eax
> 10003AF1   jmp         Emulator::~Emulator+0EAh (10003afa)
> 10003AF3   mov         dword ptr [ebp-50h],0

When you need to destruct your singleton you can use an other static
memberfunction to delete it.
Maybe something like this:
f_exit_categorize_event::free()
{
 if (--count == 0)
  delete _instance;

Quote:
}

I hope this can help you
Torsten

  Torsten.Robitzki.vcf
< 1K Download


Sat, 02 Nov 2002 03:00:00 GMT  
 Destructors/Singleton Pattern
Thanks for the help. I came to the realization that calling the first delete
(f_categorizer::delete f_exitEvent) would deallocate the memory where my
f_exit_categorize_event destructor code resides.

I plan on calling a wrapper method of f_exit_categorize_event to keep track of
the number of outstanding pointers. This destroy function will then call the
destructor when the count is nil:

eg
f_exit_categorize_event::destroy(){
    if (--count==0)
        delete this;

Quote:
}

and let
~f_Categorizer(){
    f_exitEvent->destroy();
Quote:
}

~Emulator(){
    Forward_CategorizeExit->destroy();

Quote:
}

Thanks again.....gary


Sat, 02 Nov 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Singleton Design Pattern Question

2. Singleton Design Pattern

3. How to implement Singleton pattern

4. Globalness of Singleton pattern

5. Singleton not a singleton?

6. ATL Singleton and Singleton Class also from C++

7. Pattern Definition GREP With Pattern Editing

8. Regular Expressions/Pattern Matching/Unordered pattern

9. new Singleton() ( Can I override the new operator to return a singleton?)

10. Pleading for fresh eyes on my problem using a PropertyGrid with a Singleton

11. Singletons & delete error

12. singleton class please help

 

 
Powered by phpBB® Forum Software