PC Review


Reply
Thread Tools Rate Thread

Dispose GDI+ object

 
 
#Hai
Guest
Posts: n/a
 
      9th Jul 2003
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


 
Reply With Quote
 
 
 
 
Lee Gillie
Guest
Posts: n/a
 
      9th Jul 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      9th Jul 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Vadim Melnik
Guest
Posts: n/a
 
      9th Jul 2003
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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to dispose of a Pen object Tony Johansson Microsoft C# .NET 0 17th Apr 2011 10:45 AM
object dispose laputa Microsoft Dot NET Framework 2 19th Jan 2009 08:46 AM
.net object dispose laputa Microsoft Dot NET 4 18th Jan 2009 11:43 AM
Dispose GDI+ object #Hai Microsoft Dot NET Framework 3 9th Jul 2003 08:21 PM
Dispose GDI+ object #Hai Microsoft Dot NET 3 9th Jul 2003 08:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:55 PM.