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

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.
gc_demo.zip
gcdemo.exe
gc_src.zip
DIALOG1.DSW
dialog1.dsp
#include<afxwin.h>
#include "resource.h"
#include "gcptr.h"
#include "RefCount.h"


class foo: public RefCount<foo>
{
public:
	float x;
	int y;
	void Show()
	{
		AfxMessageBox("Hello World");
	}
};

class mydialog:public CDialog
{
public:
	mydialog(int n):CDialog(n)
	{
	
	
	}

	 void OnOk()
	{
		CDialog::OnOK();

		MessageBox("YOu clicked Ok","ok");
	}

	 void OnCancel()
	{
		CDialog::OnCancel();
		MessageBox("You presses Cancel ","Cancel");
	}

};

class myframe: public CFrameWnd
{
public:
	myframe()
	{
		Create(0,"Simple Application to Check Garbage Collection",WS_OVERLAPPEDWINDOW,rectDefault,0,MAKEINTRESOURCE(IDR_MENU1));;

	}

	void OnAbout()
	{
		mydialog d(IDD_DIALOG1);
		d.DoModal();
	}

	DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)
ON_COMMAND(ID_ABOUT,OnAbout)
END_MESSAGE_MAP()


class myapp:public CWinApp
{
public:
	int InitInstance()
	{
		myframe* p=new myframe;
		p->ShowWindow(3);
		m_pMainWnd=p;
		gcPtr<foo> x=new foo;
		x->Show();
		return 1;
	}
};

myapp a;




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

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