Re: CFont memory leak !?



I built an app that implements this code. It runs at 3.3MB and never changes size. So
your leak is somewhere else.

By the way, instead of doing the Invalidate explicitly, anything that changes the behavior
should call Invalidate. So ExStatic::SetNewFont should include at the end

if(::IsWindow(m_hWnd))
Invalidate();

This allows you to call the SetNewFont on the control before it is created, but guarantees
that if you change the font, the redraw is forced. It should not be the responsibility of
the caller to force an Invalidate
joe

On 13 Dec 2006 17:33:12 -0800, "Peter Lee" <gk64higo@xxxxxxxxx> wrote:

sorry, I means 10+ MB, not 1MB...
if I don't change nPointSize, just change LOGFONT lfItalic,
lfUnderline, the memory will NOT increase...
but if the nPointSize is changed in a big wide range continuously,
memory increase until 10+MB ( memory upperbound size depend on "int
nPointSize = 1 + rand() % 60;" range )

I also found some CFont related class at www.codeproject.com, the
problem is still exist...

I don't know if this behave is correct !? windows cache the font ?

Joseph M. Newcomer ?g?D?G

(a) don't believe task manager
(b) 1MB is trivial, ignore it


On 13 Dec 2006 01:09:40 -0800, "Peter Lee" <gk64higo@xxxxxxxxx> wrote:

I look taskmgr.exe , memory takes about 1X MB, is there something wrong
with my code?


OnInitDialog()
{
SetTimer(1, 20, NULL);
}

void CMy0dlgDlg::OnTimer(UINT nIDEvent)
{

int nPointSize = 1 + rand() % 60;
m_StaticValue.SetNewFont( nPointSize, _T("Arial"));

m_StaticValue.Invalidate();
}


bool ExStatic::SetNewFont(int nPointSize, LPCTSTR lpszFaceName, CDC*
pDC /* = NULL*/ )
{

bool bRet = false;
CFont NewFont;

LOGFONT lf;

bRet = (0 != m_font.GetLogFont(&lf));
if (!bRet)
{
return false;
}

lstrcpyn(lf.lfFaceName, lpszFaceName,
sizeof(lf.lfFaceName)/sizeof(lf.lfFaceName[0]));
lf.lfHeight = nPointSize*10;

bRet = NewFont.CreatePointFontIndirect(&lf, pDC);
if (!bRet)
{
return false;
}

HFONT hNewHandle = (HFONT)NewFont.Detach();

m_font.DeleteObject();
m_font.Attach(hNewHandle);
return true;
}
****
Nothing jumps out here. I think you are being misled by what task manager is telling you.
Read my essay "How large is my program?" on my MVP Tips site. Frankly, I see absolutely
nothing worth discussing for a program as trivially small as 1MB. After you read the
article you will realize that program size as report by task manager has little to do with
how big your program is. In fact, only the first derivative matters, not the absolute
value. If the program keeps growing according to task manager, you quite possibly have a
leak. If it just 1MB, you have a rather small program, so don't worry about it.
joe
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: CFont memory leak !?
    ... the memory will NOT increase... ... but if the nPointSize is changed in a big wide range continuously, ... I think you are being misled by what task manager is telling you. ... on my MVP Tips site. ...
    (microsoft.public.vc.mfc)
  • Re: Small C# program takes up too much memory
    ... While I agree with you 100% on every technical level, the user's perception ... Users see the big number in task manager and get cranky. ... still reports a huge amount of memory, and the users perception then is that ... Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Does ShellExecute leak memory?
    ... when you see a systematic increase in the amount of memory used. ... > I observed that memory consumption for the first clickwill ... >Numega bounds checker to check for leak but it does not report any leaks. ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: Memory usage of my application
    ... leaks also very less. ... But In task manager my application is eating too much of memory usage. ... I wouldn't trust task manager numbers of memory size for anything I cared about. ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: Using CSocket with CSocketFile Increases memory usage
    ... CSocket is known to be buggy, ... application for 6 hours the memory increases in Task Manager by ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)