Click here to Skip to main content
13,197,200 members (53,116 online)
Click here to Skip to main content

Stats

87.7K views
1.1K downloads
30 bookmarked
Posted 18 May 2003

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
#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

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