473,487 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
3 4008
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

Jul 19 '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

Jul 19 '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

Jul 19 '05 #4

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

Similar topics

3
2794
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...
3
300
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...
14
3108
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
2356
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
3618
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
5729
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
4439
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
3555
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
8159
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...
0
7106
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
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
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
7181
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
7349
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
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.