473,407 Members | 2,676 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,407 software developers and data experts.

Dispose GDI+ object

Hi,
I know that in C#, destructor is a finalize method. This means that the
destructor is only called when the Garbage collector does it job. But the
the GC is always does it job when the memory is full. Is there any thing
wrong ?

If it is true, now we consider GDI object (such as Image, Graphics, Pen.
Brush or even Timer (not GDI object) ). After we use it we assign the object
to null:

Pen p = new Pen(Color.Red);
//....using pen here
p = null;

If the above code is in a loop (repeat about 1000 times), a 128MB memory is
,certainly ,not full but the resource will run out of its capacity (because
none of finalization methods are called and therefore, none of pen resources
are free). That problem is much more seriously if the resource is timer (we
have only a few timer).

However, the following code works smoothly in less than 1 second and there
is no error reported by Windows
Graphics g = CreateGraphics();
for (int i= 0; i < 1000; i++)
{
Timer t = new Timer();
t.Interval = 100;
t.Start();
t.Stop();
}

Where is my mistakes ? Can any one point it ?
Thanks
Nov 13 '05 #1
3 5911
Hi Hai -

Hmmm, not a C-sharpe' but maybe some ideas...

1) Use Dispose when it is available ( such as: p->Dispose() ) then
do
the p = null; Give it a try, & let us know. Read topics on
Dispose
and how it should be implemented for objects that use
unmanaged
stuff, and utilized to immediately free resources, such as
file handles.

2) I think declaring Timer t in the loop the way you have will
force a
Dispose on the previous t, without delay, as it wants to
replace it
immediately??

To my thinking, .NET has really obfuscated what I used to explicitly
manage,
and could trust. .NET seems to perpetuate the philosophy of
"sometimes you
need to, and sometimes you don't", and "sometimes it happens, and
sometimes
it doesn't" rather than trying to make hard rules. Now it is much
easier to not do something in some cases, and the casual read doesn't
make
it stand out. I'm not sure the code I make with it is more reliable or
faster.
Managed C++ is not C++ any more!

- HTH... Lee
" #Hai" <Re**********@Mail.com> wrote in message
news:Ol**************@TK2MSFTNGP10.phx.gbl...
Hi,
I know that in C#, destructor is a finalize method. This means that the destructor is only called when the Garbage collector does it job. But the the GC is always does it job when the memory is full. Is there any thing wrong ?

If it is true, now we consider GDI object (such as Image, Graphics, Pen. Brush or even Timer (not GDI object) ). After we use it we assign the object to null:

Pen p = new Pen(Color.Red);
//....using pen here
p = null;

If the above code is in a loop (repeat about 1000 times), a 128MB memory is ,certainly ,not full but the resource will run out of its capacity (because none of finalization methods are called and therefore, none of pen resources are free). That problem is much more seriously if the resource is timer (we have only a few timer).

However, the following code works smoothly in less than 1 second and there is no error reported by Windows
Graphics g = CreateGraphics();
for (int i= 0; i < 1000; i++)
{
Timer t = new Timer();
t.Interval = 100;
t.Start();
t.Stop();
}

Where is my mistakes ? Can any one point it ?
Thanks

Nov 13 '05 #2
There is an article in the GDI+ FAQ that deals with this subject.

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

Check out the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Buy quality Windows Forms tools
http://www.bobpowell.net/xray_tools.htm

New GDI+ articles include how to use the LinearGradientBrush and
how to draw text on a reversed Y axis for graphs and charts.

" #Hai" <Re**********@Mail.com> wrote in message
news:Ol**************@TK2MSFTNGP10.phx.gbl...
Hi,
I know that in C#, destructor is a finalize method. This means that the
destructor is only called when the Garbage collector does it job. But the
the GC is always does it job when the memory is full. Is there any thing
wrong ?

If it is true, now we consider GDI object (such as Image, Graphics, Pen.
Brush or even Timer (not GDI object) ). After we use it we assign the object to null:

Pen p = new Pen(Color.Red);
//....using pen here
p = null;

If the above code is in a loop (repeat about 1000 times), a 128MB memory is ,certainly ,not full but the resource will run out of its capacity (because none of finalization methods are called and therefore, none of pen resources are free). That problem is much more seriously if the resource is timer (we have only a few timer).

However, the following code works smoothly in less than 1 second and there
is no error reported by Windows
Graphics g = CreateGraphics();
for (int i= 0; i < 1000; i++)
{
Timer t = new Timer();
t.Interval = 100;
t.Start();
t.Stop();
}

Where is my mistakes ? Can any one point it ?
Thanks

Nov 13 '05 #3
Hi,

In addition to previous posts see also nice C# "using" statement, it
automatically generates Dispose call.

...
Regards,
Vadim.

I know that in C#, destructor is a finalize method. This means that the
destructor is only called when the Garbage collector does it job. But the
the GC is always does it job when the memory is full. Is there any thing
wrong ?

If it is true, now we consider GDI object (such as Image, Graphics, Pen.
Brush or even Timer (not GDI object) ). After we use it we assign the object to null:

Pen p = new Pen(Color.Red);
//....using pen here
p = null;

If the above code is in a loop (repeat about 1000 times), a 128MB memory is ,certainly ,not full but the resource will run out of its capacity (because none of finalization methods are called and therefore, none of pen resources are free). That problem is much more seriously if the resource is timer (we have only a few timer).

However, the following code works smoothly in less than 1 second and there
is no error reported by Windows
Graphics g = CreateGraphics();
for (int i= 0; i < 1000; i++)
{
Timer t = new Timer();
t.Interval = 100;
t.Start();
t.Stop();
}

Where is my mistakes ? Can any one point it ?
Thanks

Nov 13 '05 #4

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

Similar topics

3
by: #Hai | last post by:
Hi, I know that in C#, destructor is a finalize method. This means that the destructor is only called when the Garbage collector does it job. But the the GC is always does it job when the memory...
3
by: Rich | last post by:
I notice that lots of the GDI+ objects implement IDisposable: SolidBrush, FontFamily, Font, etc. If I don't explicitly call Dispose() on these guys after I'm done with them, what is the...
14
by: qrli | last post by:
I used to think that all objects that implement IDisposable should be disposed. But I found 80% of the classes implement IDisposable. But when I looked into the samples, most objects are not...
4
by: aaj | last post by:
Hi can anyone point to a good link explaining (simply) when I should use the dispose method. Sort of things I'd like to know are how does it differ from finalize in VB.net (or is it nothing...
14
by: Atara | last post by:
I know in C++ it is true. but what about VB .Net and its GarbageCollector ? For example, consider the following case. Does f2() needs to do any disposing code ? Public Sub f1() As...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
6
by: RG | last post by:
Hello – I’m relatively new to VB and I’m not getting how to do Dispose correctly from my readings. I have an application with 3 Forms (with a lot of logic going on within each): Form1,...
2
by: Alex K. | last post by:
Hi all In following code, is it OK not to dispose local brush explicitly before exiting the procedure? private void MyDrawItem ( Graphics g, Color BackColor, Rectangle r ) {...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
5
by: cj | last post by:
When I make a request of our sql server I Dim A New SqlConnection and I Dim A New SqlCommand. The sqlcommand gets it's connection property set to the sqlconnection. I've recently read the I need...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.