I'm not too sure why I'm getting memory leaks...hopefully someone here is wiser than me, and can help me find it :P .

#include "Canvas.h"

CDrawer CCanvas::gCanvas(RGB(0,0,0), 1);
int const GREEN(20000),BLUE(50), RED(200), THICK(3);

CCanvas::CCanvas(void): _iSize(0), _cptPoints(0)
{
}

CCanvas::CCanvas(CCanvas const & tmpCanvas): _iSize(tmpCanvas._iSize)
{
	if (_iSize > 0)
	{
		_cptPoints = new CPt [_iSize];
		for (int i(0); i < _iSize; ++i)
		{
			_cptPoints[i]._iX = tmpCanvas._cptPoints[i]._iX;
			_cptPoints[i]._iY = tmpCanvas._cptPoints[i]._iY;
			_cptPoints[i]._COLOR = GREEN;
		}
	}
}

CCanvas CCanvas::operator =(const CCanvas & RHS)
{
	if (RHS._iSize > 0)
	{
		_iSize = RHS._iSize;
		_cptPoints = new CPt [RHS._iSize];
		for (int i(0); i < _iSize; i++)
		{
			_cptPoints[i]._iX = RHS._cptPoints[i]._iX;
			_cptPoints[i]._iY = RHS._cptPoints[i]._iY;
			_cptPoints[i]._COLOR = RED;
		}
	}
	return *this;
}

CCanvas & CCanvas::Show(void)
{
	if (_iSize > 1)
	{
		gCanvas.Clear();
		for (int i(0); i < _iSize - 1; ++i)
		{
			for (int ii(i + 1); ii < _iSize; ++ii)
				if (i != ii)
					ShowPts(gCanvas, _cptPoints[i], _cptPoints[ii]);
		}
		gCanvas.Render();
	}
	return *this;
}

CCanvas::~CCanvas(void)
{
	Clean();
}

CCanvas & CCanvas::AddLines(int iX, int iY, COLORREF tmpCol)
{
	CPt * Tmp = new CPt [_iSize + 1];
	for (int i(0); i < _iSize; ++i)
	{
		Tmp[i]._iX = _cptPoints[i]._iX;
		Tmp[i]._iY = _cptPoints[i]._iY;
		Tmp[i]._COLOR = _cptPoints[i]._COLOR;
	}
	Clean();
	_cptPoints = new CPt [++_iSize];
	_cptPoints = Tmp;
	Tmp = 0;
	_cptPoints[_iSize-1]._iX = iX;
	_cptPoints[_iSize-1]._iY = iY;
	_cptPoints[_iSize-1]._COLOR = tmpCol;
	Show();
	return *this;
}

void CCanvas::Clean(void)
{
	delete [] _cptPoints;
	_cptPoints = 0;
}

this is the class where it's occurring. If anyone needs the other classes/files just ask. Thanks!

Recommended Answers

All 4 Replies

These lines here

_cptPoints = new CPt [++_iSize];
  _cptPoints = Tmp;

You allocate space for _cptPoints, and then you assign it to Tmp, which means you've now lost the memory pointer assigned by new CPt [++_iSize]; .. therefore not sure how you can delete it. That may be causing one of your leaks.

commented: Looks like a pretty good candidate for a leak to me, if only all leaks were so easy to spot. +22

But just before that is the Clean() function, which should delete the old CPt, should it not? :(

Never mind...I'm an idiot...it's fixed now! Thanks alot!

remove this line

_cptPoints = new CPt [++_iSize];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.