Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Professionals,

I have a code like this in c# winforms:

{
Font myFont = new Font("Arial",12);
.... some code with myFont

myfont = new Font("Times New Roman", 8);
.... some other code with myfont

.
.
.
}

In this case will myFont get disposed as one once out of the braces or not
or should we dispose individually ? Please guide me on this.

Thanks in advance.

What I have tried:

Tried searching on google and read documentation on garbage collection but I did not get a clear light on what I want.
Posted
Updated 1-Aug-16 3:02am

I personally would follow MSDN's suggestion: Font.Dispose Method (System.Drawing)[^]

See there Remarks, Note:
Quote:
Always call Dispose before you release your last reference to the Font. Otherwise, the resources it is using will not be freed until the garbage collector calls the Font object's Finalize method.
 
Share this answer
 
Comments
Priya-Kiko 1-Aug-16 8:10am    
Thank you for the reply.

I referred that. My doubt is should every line contain a Dispose() like this :

{
Font myFont = new Font("Arial",12);
.... some code with myFont
myFont.Dispose();

myFont = new Font("Times New Roman", 8);
.... some other code with myfont
myfont.Dispose();

.
.
.
}

or is it enough if i do myFont.Dispose() just at the end once.

Please clarify.. Thanks
[no name] 1-Aug-16 8:25am    
My Interpretation of "Always call Dispose before you release your last reference to the Font" that one should do it in each line. Why? In your example you use the same variable (reference) and so, at the time of the second myFont= ... you loose the reference for the first created font and have no Chance to dispose it.
Finally, yes dispose it like you show it in your comment.
Priya-Kiko 2-Aug-16 0:41am    
Thank you 0x01AA.
[no name] 2-Aug-16 2:47am    
You are welcome.
Maciej Los 1-Aug-16 12:04pm    
5ed!
I'd like to suggest a different approach based on the assumption that you are not creating hundreds of different instances of Fonts.

a. in Application wide, or Form wide, scope create instances of the Fonts you are going to use.

b. use them

c. when the Application closes, the Fonts are going to be garbage collected.

d. if you are really paranoid about Font disposal:
C#
using (Font arial12 = new Font("Arial",12))
{
      // use the Font
}
I have never seen a WinForms app yet that needed to explicitly dispose of Fonts.

See this discussion on StackOverFlow for further details on what a Font actually is, and issues around 'Dispose with Fonts: [^]. You might also be interested in the discussion here [^].

Note that you may have no ability to control the way some of your WinForm Controls internally manage Fonts (RichTextBox, TreeView, for example).
 
Share this answer
 
v4
Comments
Richard Deeming 1-Aug-16 10:12am    
You need an extra closing parenthesis on the first line of your code block. :)
Maciej Los 1-Aug-16 12:06pm    
using block is great alternative, a 5!
Priya-Kiko 2-Aug-16 0:52am    
Thank you for the reply.

In my case I have a main class (form) where i have written a common 'DrawItem' procedure like this to be used by all ownerdrawn listviews in other modules in my project :

public static void lvDrawItem(object sender, DrawListViewItemEventArgs e, String[] str, Int32[] x)
{
Font myfont = new Font("Segoe UI", 12);
for (int mi = 0; mi < str.Length; mi++)
e.Graphics.DrawString(str[mi], myfont, PopUpTextColor, new Point(e.Bounds.X + x[mi], e.Bounds.Y));

if ((e.State & ListViewItemStates.Focused) == ListViewItemStates.Focused)
{
e.Graphics.FillRectangle(PopUpSelectedItemBackColor, e.Bounds);
for (int mi = 0; mi < str.Length; mi++)
e.Graphics.DrawString(str[mi], myfont, PopUpSelectedItemForeColor, new Point(e.Bounds.X + x[mi], e.Bounds.Y));
}
}

So in this case I have a doubt how myfont will get disposed.

Im actually stuck with figuring out a solution for 'Error creating window handle'. In the process I figure the Fonts which im using in the loop for listview all over the project might be eating up the memory.


Please clarify.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900