TweetFollow Us on Twitter

Deferred Garbage Collection

Volume Number: 13 (1997)
Issue Number: 12
Column Tag: C++ Workshop

Deferred Garbage Collection

by Marc White

Implementing a simple deferred C++ object garbage collection class

Self Deleting Objects

When developing object oriented software using C++ there are certain situations where you may need an object that can delete itself. For instance, you might have a window or dialog class that manages its own mouse clicks and key down events. But what does the dialog object do when it determines that the user has pressed the escape or delete key, or clicked the window's close box? To stay true to the object oriented design, the window should be able to do whatever processing is needed when it determines that it is being closed and then free up the memory allocated for the object by itself.

Delete This

While the C++ syntax delete this is a legal call, it can cause serious problems if the object that makes the call is referenced in any way after the call is made. This could possibly happen while the stack is unwinding after a mouse click or key down event.

The TGarbageCollector Class

The TGarbageCollector class is a simple, drop in utility class that allows any C++ object to be safely deleted at a later time.

Listing 1: The TGarbageCollector Class

The TGarbageCollector class is a non-instance class with two public static methods: Add, and Empty, and one private static member variable: fTrashCan which is pointer to a TGarbage object.

class TGarbageCollector {
friend class TGarbage;
public:
  static void Add      ( void *trash );
  static void Empty    ( void );
  
private:
  static TGarbage      *fTrashCan;
};

TGarbageCollector::Add

The Add method of the TGarbageCollector class simply instantiates a new TGarbage object passing it a pointer to the C++ object to be deleted. Note that the trash pointer is a void pointer.

void TGarbageCollector::Add( void *trash )
{
  // create a new TGarbage object
  new TGarbage( trash );
}

TGarbageCollector::Empty

The Empty method of the TGarbageCollector class deletes all of the TGarbage objects pointed to by the fTrashCan variable. This method should be called periodically at idle time in the application's main event loop. The TGarbage object's destructor maintains the linked list.

void TGarbageCollector::Empty( void )
{
  // delete all of the items in the trash can
  while( TGarbageCollector::fTrashCan )
    delete( TGarbageCollector::fTrashCan );
}

I developed the TGarbageCollector class as a part of an application framework I was designing. The easiest way to design the garbage collection class would have been to have it delete only objects derived from a specific class, perhaps a TTrashableObject class. But, I wanted the garbage collector to be able to delete any C++ object, and I didn't want to have every class in the framework be derived from a single base class.

The TGarbage Class

This is where the TGarbage class comes in. Because the TGarbage class accepts a void pointer as a pointer to the object it will delete, any pointer can be passed into the garbage collection class to be deleted. This means that it is up to the developer (not the compiler) to make sure that the pointer passed in is a pointer to a valid C++ object.

There is one more catch which I will describe in the What's the Catch section, but for now let's take a look at the TGarbage class.

Listing 2: The TGarbage Class

The TGarbage class is a simple self-linking singly linked list class which accepts a void pointer through its constructor. It stores a pointer to the object to delete in the fTrash variable and a pointer to the next TGarbage object in the list in its fNext variable.

class TGarbage {
public:
            TGarbage    ( void *trash );
  virtual    ~TGarbage  ( void );
private:
  void        *fTrash;
  TGarbage    *fNext;
};

TGarbage::TGarbage

The TGarbage constructor stores a pointer to object to delete in its fTrash variable. Next it links itself into the linked list pointed to by the TGarbageCollector's static fTrashCan variable. It can access this private item since the TGarbageCollector has the declared the TGarbage class as a friend.

TGarbage::TGarbage( void *trash ) : fTrash( trash )
{
  // store a pointer to the next item in the chain
  if( TGarbageCollector::fTrashCan )
    this->fNext = TGarbageCollector::fTrashCan;
  else
    this->fNext = nil;
  
  // set this item as the first item in the trash can
  TGarbageCollector::fTrashCan = this;
}

TGarbage::~TGarbage

This is where the real magic happens. When the TGarbage object gets deleted we have no idea what type of object its fTrash variable points to, all that we know is that it is pointing to a C++ object. So just type cast it to a C++ class pointer (in this case a TGarbage pointer) and call the delete operator. The real object's destructor will get called and its memory will be deallocated. It's just that easy!

TGarbage::~TGarbage( void )
{
  // pull this object out of the linked list
  TGarbageCollector::fTrashCan = this->fNext;
  
  // delete the trash
  delete( (TGarbage *)this->fTrash );
}

Sample Usage

Let's take a look at the TGarbageCollector class in action. Code listing 3 shows the DoKeyDown method of a typical dialog class.

Listing 3: A Dialog Class Method

void FDialog::DoKeyDown( char theKey )
{
  switch( theKey ) {
    case kEscKey:
      // add this object to the trash
      TGarbageCollector::Add( this );
      // hide this dialog
      this->Hide();
      // do any other processing needed here before closing
      break;
    default:
      inherited::DoKeyDown( theKey );
  }
}

Notice that the dialog can add itself to the trash at any time once it determines that it needs to be deleted. The dialog object is still valid until the trash gets collected.

Next we'll take a look at the event loop method of a typical application class. This is where the TGarbageCollector's Empty method gets called and all of the objects added to the trash are deleted.

Listing 4: An Application Class Event Loop Method

void FApplication::EventLoop( void )
{
  EventRecord  theEvent;

  while( this->fQuit == false ) {
    if( WaitNextEvent( everyEvent, &theEvent, 30, nil ) ) {
      switch( theEvent.what ) {
        // handle all normal events here
        default:
          TGarbageCollector::Empty();
          break;
      }
    } else {
      TGarbageCollector::Empty();
    }
  }
}

What's The Catch?

So there has to be a catch, right? Well, of course there is. The catch is that any C++ object added to the trash must be structured in the same way as the object used to type cast the void pointer in the TGarbage object before calling its delete operator.

In simpler terms, any object added to the trash must have a virtual destructor, and its destructor must be the first virtual method of that class. This is because of the way a C++ object is structured from a class.

The TGarbage class typecasts the void pointer to the object it is going to delete into a pointer to a TGarbage object. This instructs the compiler to use the virtual table, or vtbl, of the TGarbage class to find the location of the object's destructor which it calls before deallocating the object's memory.

In the case of the TGarbage class, the destructor is the first virtual method of the class and therefore will be the first entry in the vtbl. As long as any object added to the trash can has its destructor as the first item in the vtbl, it will be properly called when the TGarbage class deletes the object.

What would happen if the object being trashed did not have a virtual destructor, or if the destructor was not the first virtual method? If the object's virtual destructor is not the first virtual method in the class, or it's destructor is not virtual, then before the memory for that object is deallocated the first virtual method of that class will be called. In the case of an object that does not have any virtual methods, the object will not even have a vtbl which means that some random memory location is going be executed as a method. Obviously, neither of these two scenarios are desirable, which is why it is very important that the developer structures the classes for trashable objects properly.

Multiple Inheritance

Can objects that use multiple inheritance be successfully deleted using the TGarbageCollector class? In short, yes, as long as they still adhere to the virtual-destructor-first method like the other classes.

However, there is an exception. Depending upon how the compiler implements multiple inheritance (I am using CodeWarrior for this example), as long as the first class specified in the inheritance chain has a virtual destructor as its first virtual method, the order of the virtual methods of the other superclasses does not matter.

Listing 5: Multiple Inheritance

// destructor is the first virtual method
class A {
public:
              A    ( void );
  virtual      ~A    ( void );
};

// destructor is the second virtual method
class B {
public:
              B    ( void );
  virtual void  Test  ( void );
  virtual      ~B    ( void );
};

// inherit from A first, then B
class C : public A, public B {
public:
              C    ( void );
  virtual      ~C    ( void );
};

// inherit from B first, then A 
class D : public B, public A {
public:
              D    ( void );
  virtual      ~D    ( void );
};

In code listing 5, class A follows the virtual-destructor-first method which would allow any object of class A to be deleted properly by the TGarbageCollector class. Class B, however, has its virtual destructor declared as the second virtual method in the class, and therefore, would not be deleted properly by the TGarbageCollector class. The B object's Test method would actually get called when the TGarbage class deleted it.

Since class C inherits from class A first and class B second, class C objects can successfully be deleted using this method. When the TGarbage class calls the C object's delete operator, the class C destructor will be called first, the class B destructor second, and the A destructor third.

Class D, however, inherits from the B class first, which means that it will behave exactly like a B object would when being deleted by the TGarbageCollector, the inherited B object's Test method will get called.

So, even though there is an exception to the virtual-destructor-first rule in the case of multiple inheritance, it is much safer to make sure that any object that might be deleted by the TGarbageCollector have its virtual destructor declared before any other virtual methods.

Summary

The TGarbageCollector class provides a simple means of adding deferred object deletion to any C++ application. However, the developer must make certain that the classes of objects which will be deleted in this manner are structured according to the virtual-destructor-first rule.


Marc White is a Macintosh programmer at US WEST Dex, Inc., where he develops client/server software used to paginate the US WEST phone directories. He can be reached at mwhite@eagle.mrg.uswest.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Senior Product Associate - *Apple* Pay (AME...
…is seeking a Senior Associate of Digital Product Management to support our Apple Pay product team. Labs drives innovation at American Express by originating, Read more
Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.