Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory leaks

Status
Not open for further replies.

timmay3141

Programmer
Dec 3, 2002
468
0
0
US
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::DrawObjects(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::Draw(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?
 
Hi timmay, rather than point you to a particular piece of your code (I'm too lazy to read it!) you may find this even more useful....

There's an MFC class called [tt]CMemoryState[/tt] - you declare one of these at the head of a function and get the state of the memory. You then get the state of the memory at the end of your function and it tells you if there's a difference.
If there is, you have a memory leak. You can then move your 'before' and 'after' bits around to narrow down and capture the object that is causing the leak! You simply use the class object to find the area that has a difference when you weren't expecting one!

Now you see why I'd rather tell you about this class rather than find the cause for you. You can use this class in any area of your project - if you try it out yourself, it'll give you valuable experience of using it!!

:)
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Well, it's not showing any memory leak. I couldn't find a leak anywhere. It is still slowing way down though. I guess it's possible the problem isn't in one of those functions, although I thought it went away when I commented them out. Could it possibly be something other than a memory leak? I have a TERRIBLE graphics card, so could the fast redraws be too much for it or something?
 
I shouldn't imaginge that that would make things keep getting slower and slower. A bad graphics card should just make it draw slow but consistent.
I'd still put my money on a leak - GDI are the most obvious but, as you say, it could well be somewhere else in your project.
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
I put CMemoryState objects at the beginning and end of the thread that is causing the problem. It's not showing a memory leak at all. So...any other ideas?
 
Sugestion:
Maybe it is another function. Check them all.
For tracking memory leaks I use &quot;Stress Utility&quot; from Visual C++ Tools. You have to watch the the GDI.
 
How do you use the stress utility? I've taken a look at it, but I'm afraid if I change something I'll screw up the program. It seems like it should be fairly obvious, I brought up the stress utility and did what slows the program down, but none of the values on the stress utility changed.
 
Why don't you try with try(..) catch(..)..try(..) finally(...)approach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top