Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A garbage collection framework for C++

, 17 Jan 2001
An article on using garbage collection in C++ through the use of smart pointers.
gctest.zip
gctest
gctest.dsp
gctest.dsw
gc_ptr.zip
#include "gcptr.h"
#include <string>
#include <iostream>

//#define	MANUAL_COLLECT
//#define	COLLECT_ON_NEW

using namespace std;

struct b1
{
	b1(const string& s) : name(s)
	{
		cout << "Creating b1(" << name << ")." << endl;
	}
	virtual ~b1()
	{
		cout << "Destroying b1(" << name << ")." << endl;
	}

	string name;
};

struct b2
{
	b2(const string& s) : name(s)
	{
		cout << "Creating b2(" << name << ")." << endl;
	}
	virtual ~b2()
	{
		cout << "Destroying b2(" << name << ")." << endl;
	}

	string name;
};

struct d1 : public b1
{
	d1(const string& s) : b1(s)
	{
		cout << "Creating d1(" << name << ")." << endl;
	}
	virtual ~d1()
	{
		cout << "Destroying d1(" << name << ")." << endl;
	}
};

struct d2 : public b1, public b2
{
	d2(const string& s) : b1(s), b2(s)
	{
		cout << "Creating d2(" << b1::name << ")." << endl;
	}
	virtual ~d2()
	{
		cout << "Destroying d2(" << b1::name << ")." << endl;
	}
};

struct circ
{
	circ(const string& s) : name(s)
	{
		cout << "Creating circ(" << name << ")." << endl;
	}
	~circ()
	{
		cout << "Destroying circ(" << name << ")." << endl;
	}

	gc_ptr<circ> ptr;

	string name;
};

void test()
{
	gc_ptr<b1> p1;

	{
		gc_ptr<d1> p2(new(gc) d1("first"));
		p1 = p2;
		gc_ptr<b1> p3(new(gc) d2("second"));
#ifdef	MANUAL_COLLECT
		gc_collect();
#endif
	}
#ifdef	MANUAL_COLLECT
	gc_collect();
#endif

	{
		gc_ptr<circ> p4(new(gc) circ("root"));
		{
			gc_ptr<circ> p5(new(gc) circ("first"));
			gc_ptr<circ> p6(new(gc) circ("second"));
			gc_ptr<circ> p7(new(gc) circ("third"));

			p4->ptr = p5;
			p5->ptr = p6;
			p6->ptr = p7;
			p7->ptr = p5;
#ifdef	MANUAL_COLLECT
			gc_collect();
#endif
		}
#ifdef	MANUAL_COLLECT
		gc_collect();
#endif
	}
#ifdef	MANUAL_COLLECT
	gc_collect();
#endif
}

int main()
{
#ifdef	COLLECT_ON_NEW
	gc_set_threshold(0);
#endif

	test();
	gc_collect();
	return 0;
}

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

William E. Kempf
Web Developer
United States United States
Windows developer with 10+ years experience working in the banking industry.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 18 Jan 2001
Article Copyright 2001 by William E. Kempf
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid