★ wanayoo — archive 1999 http://codeguru.com/cpp_mfc/SmartPtr.shtmlNouvelle recherche | Portail wanayoo
Compaq NonStop
Developer.com Click Here!
Click Here!
Find:
 

EXPERT SEARCH
-----
Site
visual c++
java
visual basic
javascripts
recommend it
 
Shopping
codeguru store
 
Contest
mfc codeguru
submission guidelines
 
Book
thinking in c++
stl programmer's guide
reviews
 
Interact
forums
newsgroups
guest book
jobs
jokes
open faq
what's new
 
Other
about
links
newsletters
privacy
tools
 

-----
Visual C++ by E-mail:

Get the weekly e-mail highlights on
Visual C++!
-----




EarthWeb Sites:
CROSSNODES
DATAMATION
DEVELOPER.COM
DICE
DISCUSSIONS
EARTHWEB.COM
EARTHWEB DIRECT
EARTHWEB EVENTS
ERP HUB
GAMELAN
GOCERTIFY
HTML GOODIES
INTRANET JOURNAL
ITKNOWLEDGE
ITLIBRARY
JARS
JAVA GOODIES
JAVASCRIPTS.COM
OPEN SOURCE IT
PERL JOURNAL
ROADCODERS
SYSOPT.COM
SUPPORTSOURCE
Y2KINFO

EarthWeb Worldwide

   

A Smart Pointer Capable of Object Level Thread Synchronization and Reference Counting Garbage Collection


This article was contributed by Stefan Tchekanov.

Introduction

The SmartPtr class is a generic wrapper that encapsulates pointers. It keeps track of the total number of reference counts and performs garbage collection when the pointed object is no more referenced by any "pointer". In addition SmartPtr can do Object Level Thread Synchronization for pointed objects. This means that calling the members of the pointed object is enclosed in Windows critical section and only one thread at a time can use the object.

SmartPtr stores reference counter and critical section in separate memory block - not in the pointed object. This causes that you don't have to inherit your classes from special base one to be able to use SmartPtr as a pointer. And one other think is that you could use SmartPtr to point objects of scalar types - int, short, long, double ....

SmartPtr is implemented as a template class. Because it has three template parameters it is annoying to specify all of them each time. This is the reason I have defined three additional template classes inherited from SmartPtr with appropriate default template parameters to get three diferent behaviours:

  • Reference Counting Garbage Collection - SmartPtr will free dynamically allocated memory automatically.
  • Synchronized Access without Reference Counting Garbage Collection - SmartPtr will perform only Thread synchronization. You should free dynamically allocated memory in your own.
  • Synchronized Access with Reference Counting Garbage Collection - SmartPtr will perform thread synchronization and will free dynamically allocated memory for you.

These are the classes:

Class Name Description
SmartPtr The SmartPtr class. It is a base class for the next classes
RefCountPtr Smart pointers that perform only Reference Counting Garbage Collection
SyncPtr Smart pointers that perform only Synchronized Access without Reference Counting Garbage Collection
SyncRefCountPtr Smart pointers that perform both Synchronized Access and Reference Counting Garbage Collection

All of these classes are equal in their implementation. The different behaviour is achieved via different default template parameters.You could use any of these four classes to get any of the three behaviours but then you should specify all parameters.

The following classes are used inside the SmartPtr implementation and SHOULD NEVER be used directly.

  • CRefCountRep
  • CSyncAccessRep
  • CSyncRefCountRep
  • CSyncAccess

Examples of how to use SmartPtr

You should include the file SmartPtr.h in your project and

#include "SmartPtr.h"

I usually include this line in my StdAfx.h file.

Let's have a class CSomething

1. Reference Counting Garbage Collection on CSomething class

class	CSomething {
	CSomething();
	~CSomething();
.....	
	void	do();
};

typedef	RefCountPtr<CSomething>	LPSOMETHING;

	
void	TestFunct() {
	LPSOMETHING	p1 = new CSomething;
	LPSOMETHING	p2 = p1;

	if( p1 == NULL ) {
	....
	}

	p2->do();
	p1 = NULL;

}//	Here the object pointed by p2 WILL BE destroyed automatically
/////////////////////////////////////////////////////////////////////////////

 

2. Object Level Thread Synchronization for objects of CSomething

class	CSomething {
	CSomething();
	~CSomething();
.....	
	void	do();
};

typedef	SyncPtr<CSomething>	LPSOMETHING;
	
void	TestFunct() {
	LPSOMETHING	p1 = new CSomething;
	LPSOMETHING	p2 = p1;

	if( p1.IsNull() ) {
	....
	}
	StartThread( p1 );

	p2->do();	//	Synchronized with the other thread
	p1 = NULL;

}	//	Here the object pointed by p2 will NOT be destroyed automatically

void	ThreadFunc( LPSOMETHING p ) {
	p->do();	//	Synchronized with the other thread
}//	Here the object pointed by p will NOT be destroyed automatically
/////////////////////////////////////////////////////////////////////////////

In this example you will get memory leaks, but the two threads will be synchronized when trying to call object's members. It is your care to free dynamically allocated memory.

 

3. Object Level Thread Synchronization and Reference Counting Garbage Collection for objects of CSomething

class	CSomething {
	CSomething();
	~CSomething();
.....	
	void	do();
};

typedef	SyncRefCountPtr<CSomething>	LPSOMETHING;
	
void	TestFunct() {
	LPSOMETHING	p1 = new CSomething;
	LPSOMETHING	p2 = p1;

	if( p1.IsNull() ) {
	....
	}
	StartThread( p1 );

	p2->do();	//	Synchronized with the other thread
	p1 = NULL;

}//	Here the object pointed by p2 WILL BE destroyed automatically
 //	if p in ThreadFunc has already released the object

void	ThreadFunc( LPSOMETHING p ) {
	p->do();	//	Synchronized with the other thread
}//	Here the object pointed by p WILL BE destroyed automatically
 //	if p2 in TestFunc has already released the object
/////////////////////////////////////////////////////////////////////////////

In this example you will not get memory leaks and the two threads will be synchronized when trying to call object's members. You don't have to free dynamically allocated memory. SmartPtr will do it for you.

 

How does SmartPtr work?

The definition of SmartPtr is:

template<class T, class REP=CRefCountRep<T>, class ACCESS = T*>
class SmartPtr {
...........
};

Where T is the type of the pointed objects, REP is the representation class used to handle the pointers and ACCESS is the class used to get thread safe access to the underlying real pointer.

There is nothing interesting about Reference Counting Garbage Collection. It is a standard implementation. The only interesting thing is that the reference counter and the real pointer are stored in the representation object instead of the pointed object.

The more interesting thing is how Object Level Thread Synchronization works. Let's look at the definition of SyncPtr class:

template<class T, class REP=CSyncAccessRep<T>, class ACCESS=CSyncAccess<T> >
class SyncPtr {
...........
	ACCESS	operator -> ();
};

Look at the reference operator. It returns object of type ACCESS, which by default is of type CSyncAccess<T>. The trick is that if the reference operator returns something different than a real pointer, the compiler calls the operator ->() of the returned object. If it returns again something different than a real pointer compiler calls the returned object operator ->(). And this continues till some reference operator returns a real pointer like T*. Well, to get object level synchronization I use this behaviour of the reference operator. I have defined a class CSyncAccess<T>:

template <class T>
class	CSyncAccess {
.............
	virtual	~CSyncAccess();
	T*	operator -> ();
};
/////////////////////////////////////////////////////////////////////////////

The representation class used in this scenario has a member of type CRITICAL_SECTION.

template <class T>
class CSyncAccessRep {
............
	CRITICAL_SECTION	m_CriticalSection;
};
/////////////////////////////////////////////////////////////////////////////

And now let's look at the example code:

typedef	SyncPtr<CSomething>	LPSOMETHING;
LPSOMETHING	p = new CSomething;

p->do();

What's going when p->do() is called?

  1. The compiler calls SyncPtr::operator->() which returns object of type CSyncAccess. This object is temporary and is stored on top of the stack. In its constructor the critical section object is initialized.
  2. The compiler calls CSyncAccess:operator->() which owns the critical section and thus protects other threads to own it and then returns the real pointer to the pointed object
  3. The compiler calls the method do() of the pointed object
  4. The compiler destroys CSyncAccess object which is on the stack. This calls its destructor where the critical section is released and other threads are free to own it.

 

The good thing about SmartPtr is that pointed objects do not have to be inherited from any special base class as in many other reference counting implementations. The reference counter and the real object pointer are stored in dinamically allocated objects of types CRefCountRep, CSyncAccessRep and CSyncRefCountRep (called representation objects) depending on what kind of SmartPtr we have. These objects are allocated and freed by SmartPtr. This approach allows us to have SmartPtr for objects of any classes, not only for inherited from special base class. Well, this is the weakness of the SmartPtr too. Imagine that you have a piece of code like this:

LPSOMETHING	p1 = new CSomething;
CSomething*	p2 = (CSomething*)p1;
LPSOMETHING	p3 = p2;

As result, at the end of this piece of code you think you will have p1 and p3 point to the same object. Yes, they will. But they will have diferent representation objects in p1 and p3. So when p3 is destroyed the underlying CSomething object will be destroyed too and p1 will point to invalid memory. So I have a simple tip: If you use SmartPtr never use real pointers to udnerlying objects like in the code above. But if you want you can still use real pointers when dealing with other objects.

Let's look at this code:

LPSOMETHING	p1 = new CSomething;
SomeFunc( p1 );

void	SomeFunc( CSomething* pSth ) {
	LPSOMETHING	p3 = pSth;
}

p3 will create its own representation, ie. its own reference counter and when SomeFunc exits p3 will free the memory pointed by p1. Instead of this function definition you'd better define it this way.

void	SomeFunc( LPSOMETHING pSth );

I talk about these to notify you that calling SmartPtr constructor or operator = with T* parameter creates new representation object and this will lead to "strange" behaviour of SmartPtr.

The mentioned above weakness of the SmartPtr behaviour could be solved if we add static table of associations - T* <-> CxxxxxRep* and every time SmartPtr gets T* we could search in this table and get appropriate CxxxRep object if it is already created. But this will lead to more memory and CPU overhead. And since now I think it is better to follow mentioned above rule than overheading.

That's all.

Download source   SmartPtr.zip (3 KB) Date Posted: 8 March 1999

Comments:

Add Comment

Give them a visit Click Here!

Click Here!

INSTANT EXPERTS!
Savvy IT Pros report on SupportSource. Take a look inside the largest vault of multi-vendor computing specs ever assembled. Click here for an In-depth Report,.

Learn about Free & Open Source software at the Bazaar Conference & Expo!,
December 14-16, 1999 in New York City! Click here for more information.

For tons of Windows technology resources, check out the Windows Programming directory.

Go to Fatbrain for the NEWEST and BEST technical books for IT professionals.

Get Linux Utilities for ONLY $34.95 at EarthWeb Direct.

Give them a visit Click Here!

Click Here!

Click Here!

Click Here!

Home About Us Search Subscribe Advertising Info Contact Us FAQs
Use of this site is subject to certain Terms & Conditions.
Earthweb's statement regarding our users' right to privacy.
Copyright ©1996-1999 EarthWeb Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited.