Click here to Skip to main content
12,689,258 members (29,576 online)
Click here to Skip to main content

Stats

84.2K views
1.1K downloads
30 bookmarked
Posted

Automatic Garbage Collection in C++ using Smart pointers

, 18 May 2003
This article explains how to prevent your programs from memory leaks, by incorporating Garbage Collector in your class.
gcdemo.exe
DIALOG1.DSW
dialog1.dsp
/*********************************************************************/
//         Written by: Muhammad Shoaib Khan
//			email:     sbk_one@yahoo.com
//        Garbage collection classes are free to use for any purpose
/**********************************************************************/


#ifndef _REF_COUNT
#define _REF_COUNT
template <class T>class RefCount
{
protected:
	int refVal;
	T* p;
public:
	RefCount(){ refVal=0; p=(T*)this;}
	void AddRef() { refVal++; }
	void ReleaseRef()
	{ 
		refVal--;
		if(refVal == 0)
			delete [] this;
		
	}	
	T* operator->(void) { return p; }
	
	
};
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

abc876
Web Developer
Pakistan Pakistan
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170113.4 | Last Updated 19 May 2003
Article Copyright 2003 by abc876
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid