timmay3141
Programmer
I'm writing a program where multiple things on the screen are constantly redrawn. It gets REALLY laggy after about 100 or so redraws, but I have found the problem is in one of a couple functions. I think it's a memory leak, because it slows down the whole computer even after you close it. I've listed the functions, one of which has a problem. Some of the parameters aren't used in the functions yet, so just ignore them. The Point3D class is a class I made for a 3D point/vector, it's just like CPoint with three dimentions and lets you multiply by scalars and such. Object is just a class to hold an object (something with mass, radius, etc., not an object of a class). ObjectNode is a class for a linked list which has a pointer to an object.
void CModelDoc:rawObjects(CDC* pDC, int nGraphMode)
{
// return if there are no objects to draw
if(ObjectNode::nCount == 0)
return;
// m_pList is the first node in the linked list
ObjectNode* pNode = m_pList;
ObjectProperties* pProps = &pNode->pObject->m_ObjProps;
for(int i = 0; i < ObjectNode::nCount; i++)
{
pNode->pObject->Draw(pDC, X_Y, Point3D(0,0,0), Point3D(0,0,0));
pNode = pNode->pNext;
if(pNode != NULL)
pProps = &pNode->pObject->m_ObjProps;
}
}
void Object:raw(CDC* pDC, int nGraphMode, Point3D ptBottomLeftFront, Point3D ptTopRightBack)
{
CBrush brColor(m_ObjProps.rgbColor);
// Get the white pen and brush for undrawing
CPen* pOldPen = (CPen*)pDC->SelectStockObject(WHITE_PEN);
CBrush* pOldBrush = (CBrush*)pDC->SelectStockObject(WHITE_BRUSH);
// Undraw the objects
if(nGraphMode == X_Y && m_ObjProps.m_wShape == SPHERE)
{
CRect rcCircle(m_ptLastPos.x, m_ptLastPos.y, m_ptLastPos.x, m_ptLastPos.y);
rcCircle.InflateRect(m_ObjProps.m_dRadius, m_ObjProps.m_dRadius);
pDC->Ellipse(rcCircle);
}
// Get the appropriate pen and brush
pDC->SelectStockObject(BLACK_PEN);
pDC->SelectObject(&brColor);
// Redraw the objects
if(nGraphMode == X_Y && m_ObjProps.m_wShape == SPHERE)
{
CRect rcCircle(m_ptPos.x, m_ptPos.y, m_ptPos.x, m_ptPos.y);
rcCircle.InflateRect(m_ObjProps.m_dRadius, m_ObjProps.m_dRadius);
pDC->Ellipse(rcCircle);
}
// Get the new redraw position
m_ptLastPos = m_ptPos;
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
brColor.DeleteObject();
}
What could cause a memory leek?
void CModelDoc:rawObjects(CDC* pDC, int nGraphMode)
{
// return if there are no objects to draw
if(ObjectNode::nCount == 0)
return;
// m_pList is the first node in the linked list
ObjectNode* pNode = m_pList;
ObjectProperties* pProps = &pNode->pObject->m_ObjProps;
for(int i = 0; i < ObjectNode::nCount; i++)
{
pNode->pObject->Draw(pDC, X_Y, Point3D(0,0,0), Point3D(0,0,0));
pNode = pNode->pNext;
if(pNode != NULL)
pProps = &pNode->pObject->m_ObjProps;
}
}
void Object:raw(CDC* pDC, int nGraphMode, Point3D ptBottomLeftFront, Point3D ptTopRightBack)
{
CBrush brColor(m_ObjProps.rgbColor);
// Get the white pen and brush for undrawing
CPen* pOldPen = (CPen*)pDC->SelectStockObject(WHITE_PEN);
CBrush* pOldBrush = (CBrush*)pDC->SelectStockObject(WHITE_BRUSH);
// Undraw the objects
if(nGraphMode == X_Y && m_ObjProps.m_wShape == SPHERE)
{
CRect rcCircle(m_ptLastPos.x, m_ptLastPos.y, m_ptLastPos.x, m_ptLastPos.y);
rcCircle.InflateRect(m_ObjProps.m_dRadius, m_ObjProps.m_dRadius);
pDC->Ellipse(rcCircle);
}
// Get the appropriate pen and brush
pDC->SelectStockObject(BLACK_PEN);
pDC->SelectObject(&brColor);
// Redraw the objects
if(nGraphMode == X_Y && m_ObjProps.m_wShape == SPHERE)
{
CRect rcCircle(m_ptPos.x, m_ptPos.y, m_ptPos.x, m_ptPos.y);
rcCircle.InflateRect(m_ObjProps.m_dRadius, m_ObjProps.m_dRadius);
pDC->Ellipse(rcCircle);
}
// Get the new redraw position
m_ptLastPos = m_ptPos;
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
brColor.DeleteObject();
}
What could cause a memory leek?