Back to the WFC main page


$Revision: 16 $


This class implements a simple garbage collector. You inherit from CGarbageCollector and your class will self destruct when the reference count reaches zero. CGarbageCollector should only be set to self destruct when it has been created via new.


void AddReference( void )
This increments the reference count and prevents the object from being deleted.
void Dump( CDumpContext& dump_context ) const
Present only in debug builds. Dumps interesting information about CGarbageCollector to the dump_context provided.
BOOL GetSelfDestruct( void ) const
Returns whether this object will self destruct when the reference count reaches zero.
long GetReferenceCount( void ) const
Returns the reference count.
void Release( BOOL * deleted = NULL )
Decrements the reference count and checks to see if it is zero. If the reference count is zero, the object will self destruct (i.e. call delete this). If deleted is not NULL, it will be filled with TRUE if the object was deleted or FALSE if there are still references to it somewhere.
void SetSelfDestruct( BOOL self_destruct = TRUE )
This should be called after CGarbageCollector has been created via new.


#include <wfc.h>
#pragma hdrstop

class CThreadData : public CGarbageCollector

      DWORD m_Data;


      void SetData( DWORD data ) { m_Data = data; };
      DWORD GetData( void ) const { return( m_Data ); };

void worker_thread( void * parameter )
   WFCTRACEINIT( TEXT( "worker_thread()" ) );

   CThreadData * thread_data = (CThreadData *) parameter;

   // Make sure the thread data is around until we call Release

   // Go do something that takes a really long time

   Sleep( 10000 );


void start_thread( void )
   WFCTRACEINIT( TEXT( "start_thread()" ) );

   CThreadData * data_p = NULL;
      data_p = new CThreadData;
   catch( ... )

   data_p->SetSelfDestruct( TRUE );

   _beginthread( worker_thread, 0, data_p );

   Sleep( 0 );


Copyright, 2000, Samuel R. Blackburn
$Workfile: CGarbageCollector.cpp $
$Modtime: 1/17/00 9:05a $