473,385 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# and GDI Resources

In MFC programming, you have to carefully check usage of GDI resources (eg
dispose of them when no longer needed).

Does the same apply to C# and .NET ?

eg if I have

Font f = new Font ();

..... use it

f = new Font ();

Do I have to put f.Dispose() in prior to the second new or does GC work it out
for you ?
Jun 6 '06 #1
10 1885
Yes, you should call Dispose() on any disposable object once you are finished
using it. By a 'disposable' object, I mean something which either implements
IDisposable or has a method with equivalent semantics; such as Close(). It
is true that the GC will dispose of such resources, but there are no
guarantees as to when or even if that will happen.

Don't forget that if you are creating and disposing of an IDisposable object
in the same lexical block, you can use C#'s using statement to ensure that
the object is disposed of regardless of whether an exception is thrown. Such
as:

using( Foo bar = new Foo() )
{
bar.DoStuff();
} // bar.Dispose() is called here.

Josh

"Ian Semmel" wrote:
In MFC programming, you have to carefully check usage of GDI resources (eg
dispose of them when no longer needed).

Does the same apply to C# and .NET ?

eg if I have

Font f = new Font ();

..... use it

f = new Font ();

Do I have to put f.Dispose() in prior to the second new or does GC work it out
for you ?

Jun 6 '06 #2
Ian Semmel wrote:
In MFC programming, you have to carefully check usage of GDI resources
(eg dispose of them when no longer needed).

Does the same apply to C# and .NET ?

eg if I have

Font f = new Font ();

.... use it

f = new Font ();

Do I have to put f.Dispose() in prior to the second new or does GC work
it out for you ?


Yes, you should explicitly dispose of any IDisposable objects (or wrap
in a using statement).

JB
Jun 7 '06 #3
_DD
On Tue, 6 Jun 2006 15:58:02 -0700, Josh Smith
<Jo*******@discussions.microsoft.com> wrote:
Yes, you should call Dispose() on any disposable object once you are finished
using it


I'm curious about this. What happens if you don't call dispose...what
are the usual symptoms?

Also, is there any way to set a 'marker' when a suspicious program
runs so that resources can be reclaimed afterward?
Jun 7 '06 #4
Memory leaks, dabatbase connections which hang open until they
timeout..

It can be pretty nasty actually. Be very careful to dispose any object
implementing IDisposable. In practice, even classes which have a Close
method implement this interface (and indeed, close just usually calls
dispose).

Andy

_DD wrote:
On Tue, 6 Jun 2006 15:58:02 -0700, Josh Smith
<Jo*******@discussions.microsoft.com> wrote:
Yes, you should call Dispose() on any disposable object once you are finished
using it


I'm curious about this. What happens if you don't call dispose...what
are the usual symptoms?

Also, is there any way to set a 'marker' when a suspicious program
runs so that resources can be reclaimed afterward?


Jun 7 '06 #5


John B wrote:
Ian Semmel wrote:
In MFC programming, you have to carefully check usage of GDI resources
(eg dispose of them when no longer needed).

Does the same apply to C# and .NET ?

eg if I have

Font f = new Font ();

.... use it

f = new Font ();

Do I have to put f.Dispose() in prior to the second new or does GC
work it out for you ?

Yes, you should explicitly dispose of any IDisposable objects (or wrap
in a using statement).

JB


But suppose I have a function say

void fn ( Font f )
{
if ( condition )
{
f.Dispose ();
f = new Font ();
}
....
}

How do I know that f is not being used somewhere else ?
Jun 7 '06 #6
The object that creates the instance is the one that must dispose of
it, usually. So you wouldn't dispose of any parameters which are
passed in; that would be the callers responsibility.

Ian Semmel wrote:
But suppose I have a function say

void fn ( Font f )
{
if ( condition )
{
f.Dispose ();
f = new Font ();
}
....
}

How do I know that f is not being used somewhere else ?


Jun 7 '06 #7
_DD
On 7 Jun 2006 10:40:56 -0700, "Andy" <aj*****@alum.rit.edu> wrote:
Memory leaks, dabatbase connections which hang open until they
timeout..

It can be pretty nasty actually. Be very careful to dispose any object
implementing IDisposable. In practice, even classes which have a Close
method implement this interface (and indeed, close just usually calls
dispose).


Andy, Thanks for your reply. I was particularly interested in regard
to graphics resources. All programs (incl old Win32) can allocate
them, and it seems there are lots of places for things to go wrong. I
guess my primary questions would be:

When graphics resources run out, what are the usual symptoms shown at
operating system level?

Can anything be done to correct this after it happens, short of a
reboot?
Jun 8 '06 #8
That's interesting. I was working on the assumption that in C# and .NET that
anything goes, but it appears that not everything is so straight forward.

The trouble with C# is that it is so high a level, that it is not always clear
what is going on behind the scenes (cf MFC where you are working directly with
the dlls).

It could be that over time, there will be a lot of pretty wild and wooly .NET
programs out there.

Andy wrote:
The object that creates the instance is the one that must dispose of
it, usually. So you wouldn't dispose of any parameters which are
passed in; that would be the callers responsibility.

Ian Semmel wrote:
But suppose I have a function say

void fn ( Font f )
{
if ( condition )
{
f.Dispose ();
f = new Font ();
}
....
}

How do I know that f is not being used somewhere else ?


Jun 8 '06 #9
DD,

Likely memory leaks, but there may be other symptoms as well, although
I don't know what they might be off-hand.

A reboot would certainly 'fix' the problem. I'm not sure how else the
system would know to reclaim the memory. I'm going to reply to the
next person in this thread; that may have more info for you.

Andy

_DD wrote:
Andy, Thanks for your reply. I was particularly interested in regard
to graphics resources. All programs (incl old Win32) can allocate
them, and it seems there are lots of places for things to go wrong. I
guess my primary questions would be:

When graphics resources run out, what are the usual symptoms shown at
operating system level?

Can anything be done to correct this after it happens, short of a
reboot?


Jun 8 '06 #10
Nope, not anything goes. Like all programming, there are rules and
gotchas.

I'm pretty sure that all objects which implement the IDisposable
interface use unmanaged resources; i.e. they end up being a wrapper to
a COM DLL call, which .Net cannot know what other programs are using
that particular COM object server. So .Net can't free unmanaged
resources automatically, because other applications may actually be
using those same resources.

The rule is, if the object has a Dispose or Close method, make SURE you
call it when you are done with it. If you're using anything from
System.IO, System.Windows.Forms, and System.Data.* you'll defaintly
come across such classes. There are others throughout the framework as
well.

As long as you follow that rule, you'll be fine. As an example, if
your class opens a database connection when an object is instanciated
from that class and keeps the connection around the entire lifetime of
your object, you should implement IDisposable as well. Your dispose
method would make sure to Dispose the database connection. Its up to
the client of your object to call Dispose on your object.

I think that if you don't call Dispose, eventually it will be called
when the garbage collector finalizes the objects, but that is a HUGE
performance hit, and in the case of a database connection, you keep a
connection open much longer than you really need to. So you shouldn't
rely on the finalizer.

I had a bug in my custom Data access layer used by my application where
my SqlDataReaders weren't being disposed after I was done using them.
What ended up happening was that each instance of my application
started using about 10 - 20 different connections to the Sql server, to
the point where the server wasn't responding as well and data
operations started getting exceptions when it tried to open a new
connection. This happened pretty quickly because even though I had a
few users, every 30 seconds the app would hit the database. The bug
was in my data layers Find code; I wasn't disposing the DataReader when
I was done. I just put the data reader into a using( ) {} block,
recompiled and distributed the new application.. now the app uses one
connection (via connection pooling) to do its work, there are no more
errors and the server is very responsive again. The moral is to always
make sure to dispose your disposable objects, using the using block (in
C#, VB has an equivelent).

HTH
Andy

Ian Semmel wrote:
That's interesting. I was working on the assumption that in C# and .NET that
anything goes, but it appears that not everything is so straight forward.

The trouble with C# is that it is so high a level, that it is not always clear
what is going on behind the scenes (cf MFC where you are working directly with
the dlls).

It could be that over time, there will be a lot of pretty wild and wooly .NET
programs out there.


Jun 8 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Derick Smith | last post by:
Hi! I am just starting to use .NET and need some help! If I create my own DLL for String resouces using this command: C:\WINNT\MICROS~1.NET\Framework\v1.1.4322\Al /t:lib...
3
by: Jesse | last post by:
Hi together, I've a problem with compiling an application with a build-Script and run it after. Several resource-files I compile with resgen.exe and put the files into a folders of the...
1
by: Stefan Turalski \(stic\) | last post by:
Hi, What I need to do is adding some support for resources files to my application. What I did is: MyAppMain <- startup project MyAppHelper <- project which has MyAppResourcesClass (al a...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
4
by: Jon Rista | last post by:
I have a project where I need to create a windows .exe by compiling code and linking in some resources. This program thats being generated is somewhat unconventional, and I'll explain how. I'm...
0
by: Johann Blake | last post by:
I'm having trouble grasping how ASP.NET correctly locates resources. There is plenty of documentation on this subject but some things are not clear at all. In my ASP.NET application, I have...
0
by: Derick Smith | last post by:
Hi! I am just starting to use .NET and need some help! If I create my own DLL for String resouces using this command: C:\WINNT\MICROS~1.NET\Framework\v1.1.4322\Al /t:lib...
0
by: Rob Dob | last post by:
Hi, I have a VS2003 C# asp.net project that has been converted into a VS2005 project. Everything seemed to work well until I make a modification to anything within the Component Designer...
1
by: spencermiles | last post by:
Hello, I'm working on a large solution, comprised on numerous Projects, and I would like to have one central projects that contains a set of global RESX Resources. It doesn't make sense to have...
0
by: shamirza | last post by:
· When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.