Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Not disposing StringFormat?

37 views
Skip to first unread message

JS

unread,
Jan 27, 2008, 7:36:27 AM1/27/08
to
Can anyone tell me possible side effects of not disposing StringFormat
instances?

I have some software I've released where I don't dispose these (note,
MSDN has lots of examples where they create StringFormat instances
without disposing them).

I have changed my software so that future releases will dispose these
objects, but in order to support existing customers, I'm wondering if
anyone knows of symptoms I should watch for from customer problem
reports which might point to the StringFormat not being disposed.

Thanks.

Bob Powell [MVP]

unread,
Jan 27, 2008, 9:09:08 AM1/27/08
to
StringFormats are often drawn from the static objects
StringFormat.GenerigDefault and StringFormat.GenericTypeographic.

These should not be modified or disposed because this can adversely affect
all the text in the application.

When you create an object from scratch and that object is disposable then
you should call dispose as a matter of course. Microsoft examples often
don't, this has been a bugbear of mine for many years.

If you need to use a modified string format based on one of the standard
ones you should clone, for example, GenericTypeographic, modify it, use it
and dispose of it.


--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


"JS" <jnosp...@gmail.com> wrote in message
news:bef7c956-aeb1-4627...@d21g2000prf.googlegroups.com...

JS

unread,
Jan 27, 2008, 10:05:25 AM1/27/08
to
Thanks Bob. I hadn't noticed the static properties on StringFormat,
but it makes sense that you'd want to clone these before modifying.
I've always done a new StringFormat() and then set it up. The problem
was, I didn't notice that StringFormat was disposable (and the
Microsoft examples didn't help). It would be nice if Intellisense or
the VS editor highlighted the IDisposable objects somehow.

The current release of my software is not disposing some of these
objects -- I am guessing that there could be several thousand
StringFormat objects that I have not released because I found one of
them in the OnPaint of a UserControl that I wrote. I'm not sure how
serious of a problem this is.

Bob Powell [MVP]

unread,
Jan 27, 2008, 11:49:45 AM1/27/08
to
Its good form to dispose of any object that you create if it is disposable.
In the case of many GDI+ objects, StringFomat included, the System.Drawing
classes are in fact nothing more than a wrapper around the unmanaged GDI+
library. This means that the management of unmanaged objects is important
especially if you want good graphical performance.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


"JS" <jnosp...@gmail.com> wrote in message

news:e0e125fd-f916-41ff...@s12g2000prg.googlegroups.com...

0 new messages