473,811 Members | 4,047 Online
Bytes | Software Development & Data Engineering Community
+ 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 1356
"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*********@cu be.net> wrote in message
news:Ox******** ******@TK2MSFTN GP11.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******** ********@TK2MSF TNGP10.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*********@cu be.net> wrote in message
news:Ox******** ******@TK2MSFTN GP11.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******** ********@TK2MSF TNGP10.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
6498
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 -------------- using System.ComponentModel; ....
6
2543
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; using System.ComponentModel; using System.Windows.Forms; using System.Data;
2
1997
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.IContainer components = null; public Form1() { // This call is required by the Windows Form Designer.
0
1223
by: afatdog | last post by:
Form1: //----------------------------------------------------------------- public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.IContainer components = null; public Form1() { // This call is required by the Windows Form Designer.
0
1292
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 right group to ask my second question, if not I'll try the InfoPath group but I'll throw it in anyways. From the InfoPath side, I've seen a couple different ways to receive the data such as: http://support.microsoft.com/?id=826994...
4
1124
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
1627
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 Form2, I would expect to click on the second Form1 button and get intMyValue = 0. Form2 has code to reinitialize it in its close event, but this doesn't seem
0
1482
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 combobox is selected via mouse click it draws the dropdown list twice. As I am trapping the enter event, I can see thats is called as well some other ? the style is set to dropdownlist as they must select one from the list I assume I must trap a...
4
3334
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, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
1
223
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 exist on another form (not frmTest). Button1 attempts to run sub Test with another thread, which Button2 simply attempts to run sub Test. The results, in short, are the Button1 does not work while Button2 does. Also, once Button1 is run, neither...
0
9724
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10394
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10127
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9201
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7665
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4336
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 we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.