Page 1 of 1

Is it still required to destroy Font object ?

PostPosted: Tue Nov 15, 2005 10:10 am
by Milan Mehta
Hi,

Is it still required to destroy Font object to release memory occupied by it ? I am using Jun-2005 build.

TIA
Milan.

Re: Is it still required to destroy Font object ?

PostPosted: Tue Nov 15, 2005 11:11 am
by Enrico Maria Giordano
Yes for fonts created by yourself and used "on the fly", ie. not assigned to any controls.

EMG

Re: Is it still required to destroy Font object ?

PostPosted: Mon Jan 04, 2010 9:39 pm
by Otto
Does this mean if the program is terminated with oWnd:end() that the FONT is still occupying the memory?
Is there a function to know which Fonts one have ceated?

Thanks in advance
Otto

Re: Is it still required to destroy Font object ?

PostPosted: Mon Jan 04, 2010 10:00 pm
by Antonio Linares
Otto,

In this thread we explain how to check all created GDI objects:
viewtopic.php?f=3&t=15935&start=0&hilit=gdi+resources

Re: Is it still required to destroy Font object ?

PostPosted: Mon Jan 04, 2010 10:22 pm
by Otto
Antonio,
I don’t understand exactly when a FONT has to be released.
If I look into the new tribbon classe I see that ::oFont is created.
How is this Font released?

If I use for example:
Func test(oFont)
Local oFont1
oFont1:=oFont
return nil

Do I have 2 fonts and must I release oFont1?
Thanks in advance
Otto

Re: Is it still required to destroy Font object ?

PostPosted: Mon Jan 04, 2010 10:40 pm
by Antonio Linares
Otto,

If you mean this font in Class TRibbonBar:
Code: Select all  Expand view

   if ::oFont == nil
      ::oFont := TFont():New()
      ::oFont:hFont := GetStockObject( DEFAULT_GUI_FONT )
   endif
 

there is no need to destroy it as it uses a Windows own hFont.

If you create a font using:
Code: Select all  Expand view

DEFINE FONT oFont NAME ... SIZE ..., ...
 

then after is used by an object (window, dialog, control, printer) you have to destroy it yourself:
Code: Select all  Expand view

oFont:End()
 

Fonts reuse their hFont handles to reduce GDI memory consume, using a counter which gets incremented when the font is assigned to an object and decremented when the object is End()ed. But when we do oFont:End() then the counter is decreased to zero and then it is really destroyed

Re: Is it still required to destroy Font object ?

PostPosted: Mon Jan 04, 2010 10:59 pm
by Otto
Hello Antonio,

Thank you for your help.

I use 2 methods to paint my booking plan:
paint + paintChart
Depending on some conditions I use
::paintChartFont := ::oFont
Or
::paintChartFont := ::planFont

Does this assignment always create a new font ?
Thanks in advance
Otto

Code: Select all  Expand view
METHOD paint()

   do while .not. EOF()
      IF PLAN_DB->jahrgang4 = "MELD"
         ::paintChartFont := ::oFont
       else
         ::paintChartFont := ::planFont
       ENDIF

      ::PaintChart(  )
      skip

   ENDDO

return nil
//----------------------------------------------------------------------------//

METHOD PaintChart()
   hOldFont := SelectObject( ::oWnd:hDC, ::paintChartFont:hFont )

   DrawText( ::oWnd:hDC, cChartCaption,{ nCurRow - 6 ,;
      StartPX + nTemp ,;
      nCurRow + ::nRowHeight ,;
      StartPX + nDurationPX + ::nColWidth/2 - 2  }, nStyle  )

   SelectObject(  ::oWnd:hDC, hOldFont )
   DeleteObject( hOldFont )
return nil

//----------------------------------------------------------------------------//
 

Re: Is it still required to destroy Font object ?

PostPosted: Tue Jan 05, 2010 12:07 am
by James Bott
Otto,

No, you are just creating an alias for the same font object.

To create a copy of an object:

oNew := oClone( oObject )

James

Re: Is it still required to destroy Font object ?

PostPosted: Tue Jan 05, 2010 7:03 am
by Otto
James,
Thank you James for explaining this to me.
Best regards,
Otto

Re: Is it still required to destroy Font object ?

PostPosted: Tue Jan 05, 2010 7:20 am
by Antonio Linares
James, Otto,

Please be aware that oClone() should not be used with Windows handles as those can't be "cloned" :-)

Re: Is it still required to destroy Font object ?

PostPosted: Tue Jan 05, 2010 4:58 pm
by James Bott
Antonio,

Thanks for pointing that out. Very important!

James