NT...Win9X...problem.....memory leak???
-
Hi, Thanks for all of your reply about my question before. THANK YOU VERY MUCH!!!! I try to keep posting my question, so that someone new can see this question and tell me how to fix it.... My major problem is: I have a program that was implemented in NT before and now want to transfer to Win9x. Everything is fine in NT, but in Win9x after the program has run for awhile, the output font change...include the system message box ( i know it because there is a message box ask me to terminate the program..haha..)......but it takes awhile to see the problem........when i do drawing....the problem happened immediately.....( not just draw a line......but a heavy drawing....).....cannot display colour....etc... I posted this question in codeproject.....and many people told me it would be a GDI resource leak....but i don't know how to fix it.... Oh...give you more backgrounds......it is a program using MFC...........and has a CDC global pointer to let other files to do drawing too...(not in view.cpp only....).....I'm just used some liked CPen or CBrush...then selectobject.....then something liked LineTo or MoveTo..... Thanks for your help..... Richard
-
Hi, Thanks for all of your reply about my question before. THANK YOU VERY MUCH!!!! I try to keep posting my question, so that someone new can see this question and tell me how to fix it.... My major problem is: I have a program that was implemented in NT before and now want to transfer to Win9x. Everything is fine in NT, but in Win9x after the program has run for awhile, the output font change...include the system message box ( i know it because there is a message box ask me to terminate the program..haha..)......but it takes awhile to see the problem........when i do drawing....the problem happened immediately.....( not just draw a line......but a heavy drawing....).....cannot display colour....etc... I posted this question in codeproject.....and many people told me it would be a GDI resource leak....but i don't know how to fix it.... Oh...give you more backgrounds......it is a program using MFC...........and has a CDC global pointer to let other files to do drawing too...(not in view.cpp only....).....I'm just used some liked CPen or CBrush...then selectobject.....then something liked LineTo or MoveTo..... Thanks for your help..... Richard
I posted this advice before, and I repeat it: you should get rid of the global DC. Just pass the CDC pointer from OnDraw to functions defined in other .cpp files. Your GDI leaks are (most probably) caused by the fact that pens, brushes and other GDI objects can't be freed when selected into any device context.
void Leak(CDC *pDC)
{
CPen pen(...);
pDC->SelectObject(&pen);
// ... draw here
}CPen's destructor will call ::DeleteObject for you, but it will fail - your pen is still selected. You should re-select before exiting the function:
void TheRightStuff(CDC *pDC)
{
CPen pen(...);
CPen *oldPen = pDC->SelectObject(&pen);
// ... draw here
pDC->SelectObject(oldPen);
}Tomasz Sowinski -- http://www.shooltz.com.plC
-
I posted this advice before, and I repeat it: you should get rid of the global DC. Just pass the CDC pointer from OnDraw to functions defined in other .cpp files. Your GDI leaks are (most probably) caused by the fact that pens, brushes and other GDI objects can't be freed when selected into any device context.
void Leak(CDC *pDC)
{
CPen pen(...);
pDC->SelectObject(&pen);
// ... draw here
}CPen's destructor will call ::DeleteObject for you, but it will fail - your pen is still selected. You should re-select before exiting the function:
void TheRightStuff(CDC *pDC)
{
CPen pen(...);
CPen *oldPen = pDC->SelectObject(&pen);
// ... draw here
pDC->SelectObject(oldPen);
}Tomasz Sowinski -- http://www.shooltz.com.plC
Oh.....i understand that now......let me try.... Thanks, Tomaxz....... THANK YOU THANK YOU THANK YOU!!!!!;P