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

Question about show forms modally and calling the Dispose() method

I have always read that forms that are shown modally through ShowDialog()
need to be disposed manually. Because they are not disposed automatically
since you can still access the form after the ShowDialog() call.

We were having a discussion about why you need to call the Dispose(). I
thought that the garbage collector does not calls the destructor because of
the window handle. Can someone tell me what the real reason is that you need
to manually dispose() a modally shown form?

What happens if you don't dispose the form, won't the garbage collector
destroy the form on the managed heap or will you have leaks or what?

Gabriel Lozano-Morán
Nov 23 '05 #1
6 1384
Gabriel,

The showdialog uses the internal dialogform from Windows.

However you can as well other forms show after the close call by asking

if myform.IsDisposed <> False then
if (myform.IsDisposed!=false) {

and then show or otherwise create it new.

Does that explain it in this short way?

Cor
Nov 23 '05 #2
Hello Cor

I am wondering what will happen if you do not call the dipose method and the
application exits. Will there be a memory leak or not?

Gabriel

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:O0**************@TK2MSFTNGP12.phx.gbl...
Gabriel,

The showdialog uses the internal dialogform from Windows.

However you can as well other forms show after the close call by asking

if myform.IsDisposed <> False then
if (myform.IsDisposed!=false) {

and then show or otherwise create it new.

Does that explain it in this short way?

Cor

Nov 23 '05 #3
Gabriel,

The (not overloaded) dispose methode does in fact almost nothing.

It is a signal for the GC that the object is in your opinion ready for
releasing.

If that is not the case than the GC will ignore it and should do it himself
if he finds that it can be done, if you dispose it or not.

One of the main purposes of the managed code is to prevent memoryleaks.

Cor
Nov 23 '05 #4
If you do not dispose the form the controls Finalizer() will be called
during garbage collection. The Finalizer() calls the Dispose() method
passing false. The destroying of the window handle is done when true is
passed to the Dispose() method wiht a call to DestroyHandle(). This mean
that if you do not call the Dispose(true) method the handle and the window
(and child windows, timers, ...) will never be destroyed? If so how much
memory will be leaked?

Gabriel

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Gabriel,

The (not overloaded) dispose methode does in fact almost nothing.

It is a signal for the GC that the object is in your opinion ready for
releasing.

If that is not the case than the GC will ignore it and should do it himself if he finds that it can be done, if you dispose it or not.

One of the main purposes of the managed code is to prevent memoryleaks.

Cor

Nov 23 '05 #5
Calling Dispose manually releases all resources. If it gets called by the
finalizer then it only releases unmanaged resources, as managed resources get
finalized anyway.
Nov 24 '05 #6
Reuben,
Calling Dispose manually releases all resources.


Did you ever try this
\\\
Show()
Dim ds As New DataSet
Text = "0"
Do While 0 = 0
ds.Dispose()
Text = (CInt(Text) + 1).ToString
Show()
Loop
///

Is it not weird as what you say is true, that you can dispose an object
endless times while it is released?

(What you write I have nowhere seen written officialy by the way, only on
forums and blogs, something as that babies comes from everywhere except from
where they really come from)

Cor
Nov 24 '05 #7

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

Similar topics

11
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the...
4
by: Mika M | last post by:
Hi! If Windows Form -application has for example a button on Form1 which Click-event opens other Form2-form like... Dim f as New Form2 f.ShowDialog() If (f.DialogResult = DialogResult.OK)...
7
by: Michael Erlewine | last post by:
I am trying to learn VB.Net and can't figure out how to call a from from another form. In VB6, I just issue a Load FormX, and an Unload FormX... and Show the form and there it is. I am sure it...
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...
7
by: rforman1 | last post by:
Suppose I do this: let's say that in my vb.net app's main/MDI form, I declare a form-level variable of type Form1, where Form1 is another form (more technically, I know, another class which itself...
21
by: Roland | last post by:
The following issue is puzzling me: There are 2 ways of writing the code below: .... Dim fnt as Font = New Font(...) DrawString(myText, fnt,...) fnt.dispose(). or DrawString(myText, New...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
1
by: Flack | last post by:
Hey guys, I'm having some weird issues when calling Dispose on one of the controls in a user control from an asycn or sync method. When called form an async method Dispose makes a call to...
8
by: MikeY | last post by:
Hi Everyone, I'm lookig for a way around retrieving my information, with out having to take it to another method. I have two buttons "YES" "NO" on a custum control and I want to retrieve the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.