473,471 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

question of disposing

I have two questions for which I couldnt find answer:

If I programaticaly close DialogForm (calling Close()), is it enough or do I
have to dispose it as MS.NET help says?

Also, in overriden onPaint method, do I have to dispose pens, brushes and
graphics object or do they got disposed by Framework?
Nov 16 '05 #1
5 1340
"Mathias L." <ma************@nonospamgxm.de> wrote in
news:ci**********@ls219.htnet.hr...
I have two questions for which I couldnt find answer:

If I programaticaly close DialogForm (calling Close()), is it enough or do
I
have to dispose it as MS.NET help says?
Quoted from MSDN, Remarks section about Form.Close:
"When a form is closed, all resources created within the object are closed
and the form is disposed..."
Does that answer your question?
Also, in overriden onPaint method, do I have to dispose pens, brushes and
graphics object or do they got disposed by Framework?


Members of the Pens and Brushes classes will be cleaned by the framework;
You have to clean all the classes you create yourself.

Niki
Nov 16 '05 #2
Niki,

Resources such as brushes created by the form might be disposed (I have not
checked) but I don't believe managed objects/components contained on the
form are. Certainly any objects not added to the components object are not.
Unlike some framework classes Close() does not directly call Dispose(), they
are not the same.

I would suggest that it is best (but not necessarily mandatory) to call
myForm.Dispose() to ensure all managed objects are disposed as soon as
possible. Personally if an object exposes a Dispose() method I always try to
call it at an appropriate time.

The pens/ brushed you have created are I assume the managed objects exposed
by the framework, so they will at some point be reclaimed automatically by
the garbage collection. But to ensure this is done sooner rather than later
then yes I would call their Dispose method as soon as possible, perhaps
doing so in the forms Dispose method. If you don't then the underlying
windows handle will be retained until the GC decides to finally release
them.

Phil...

"Niki Estner" <ni*********@cube.net> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
"Mathias L." <ma************@nonospamgxm.de> wrote in
news:ci**********@ls219.htnet.hr...
I have two questions for which I couldnt find answer:

If I programaticaly close DialogForm (calling Close()), is it enough or
do I
have to dispose it as MS.NET help says?


Quoted from MSDN, Remarks section about Form.Close:
"When a form is closed, all resources created within the object are closed
and the form is disposed..."
Does that answer your question?
Also, in overriden onPaint method, do I have to dispose pens, brushes and
graphics object or do they got disposed by Framework?


Members of the Pens and Brushes classes will be cleaned by the framework;
You have to clean all the classes you create yourself.

Niki

Nov 16 '05 #3
My previous positing should have been addressed to Mathias not Niki.

Phil...
"Phil Jenson" <Ph**@jenson.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Niki,

Resources such as brushes created by the form might be disposed (I have
not checked) but I don't believe managed objects/components contained on
the form are. Certainly any objects not added to the components object are
not. Unlike some framework classes Close() does not directly call
Dispose(), they are not the same.

I would suggest that it is best (but not necessarily mandatory) to call
myForm.Dispose() to ensure all managed objects are disposed as soon as
possible. Personally if an object exposes a Dispose() method I always try
to call it at an appropriate time.

The pens/ brushed you have created are I assume the managed objects
exposed by the framework, so they will at some point be reclaimed
automatically by the garbage collection. But to ensure this is done sooner
rather than later then yes I would call their Dispose method as soon as
possible, perhaps doing so in the forms Dispose method. If you don't then
the underlying windows handle will be retained until the GC decides to
finally release them.

Phil...

"Niki Estner" <ni*********@cube.net> wrote in message
news:Ox**************@TK2MSFTNGP11.phx.gbl...
"Mathias L." <ma************@nonospamgxm.de> wrote in
news:ci**********@ls219.htnet.hr...
I have two questions for which I couldnt find answer:

If I programaticaly close DialogForm (calling Close()), is it enough or
do I
have to dispose it as MS.NET help says?


Quoted from MSDN, Remarks section about Form.Close:
"When a form is closed, all resources created within the object are
closed and the form is disposed..."
Does that answer your question?
Also, in overriden onPaint method, do I have to dispose pens, brushes
and
graphics object or do they got disposed by Framework?


Members of the Pens and Brushes classes will be cleaned by the framework;
You have to clean all the classes you create yourself.

Niki


Nov 16 '05 #4
"Phil Jenson" <Ph**@jenson.co.uk.nospam> wrote in
news:%2****************@TK2MSFTNGP10.phx.gbl...
Niki,

Resources such as brushes created by the form might be disposed (I have
not checked) but I don't believe managed objects/components contained on
the form are. Certainly any objects not added to the components object are
not. Unlike some framework classes Close() does not directly call
Dispose(), they are not the same.
Ooops, you're right! Close doesn't call Dispose if the form was shown with
ShowDialog.
I would suggest that it is best (but not necessarily mandatory) to call
myForm.Dispose() to ensure all managed objects are disposed as soon as
possible. Personally if an object exposes a Dispose() method I always try
to call it at an appropriate time.
I think that's the right way, too.
The pens/ brushed you have created are I assume the managed objects
exposed by the framework, so they will at some point be reclaimed
automatically by the garbage collection. But to ensure this is done sooner
rather than later then yes I would call their Dispose method as soon as
possible, perhaps doing so in the forms Dispose method. If you don't then
the underlying windows handle will be retained until the GC decides to
finally release them.


Pens/Brushes are often created on demand in a draw method, and it's a bad
habit not to dispose such temporary objects immediately (e.g. with a using
block). They will be cleaned up by the GC, but the GC only sees an object of
about 16 bytes size (the managed object) and doesn't know about the
unmanaged (probably much bigger) costs of it, so it might well keep it in
memory far longer than it's good.

Niki
Nov 16 '05 #5
Niki ,
Pens/Brushes are often created on demand in a draw method, and it's a bad
habit not to dispose such temporary objects immediately (e.g. with a using
block).


I thought it was common for an application to create a cache of frequently
used pens/brush used (similar to the stock objects) to reduce to overhead of
creating/disposing when the are used regualry. Obvioulsy you need to get the
right balance, but I would have thought it acceptable to hold on to them
until a form is disposed, given the paint event fires so many times.

Having said that in principle I do agree with you as there are many (simiar)
objects requiring a windows handle, that makes it impratcile to hold on to
each.

Phil...
Nov 16 '05 #6

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

Similar topics

10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
6
by: Vanessa | last post by:
With this program I can do one selection, but upon the second I get an error where ///////////////// is indicated. Please help. using System; using System.Drawing; using System.Collections;...
2
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private...
0
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private...
0
by: Terry-OMAF | last post by:
I'm trying to create a web service in C# to populate a drop down in MS InfoPath with Active Directory users. How do I return what's found (if possble please provide code)? Not sure ift's the...
4
by: Raed Sawalha | last post by:
I have a SqlConnection object , Dose calling Dispose() in SqlConnection Object close the opened connection to database? Other: When I need to implement IDisposal interface?
2
by: Bert Szoghy | last post by:
Hello, I am missing something about Visual Basic .NET module variables and window close events. In the following code, after opening Form2 by clicking a button on Form1 and then closing...
0
by: | last post by:
Hi All. I have a problem with combobox, what I want is when combobox gets focus I need it to show the dropdown list t.This is fine if the user selects the combobox via keystrokes but when the...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
1
by: Aaron | last post by:
Hi, 1. Can a seperate thread create a form? I created some test code. I have a sub Test that attempts to create and show a form, objFrmTest which is of type frmTest. Button1 and Button2...
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...
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...
1
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...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.