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

  • Thread starter Gabriel Lozano-Morán
  • Start date
G

Gabriel Lozano-Morán

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
 
C

Cor Ligthert [MVP]

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
 
G

Gabriel Lozano-Morán

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
 
C

Cor Ligthert [MVP]

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
 
G

Gabriel Lozano-Morán

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
 
G

Guest

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.
 
C

Cor Ligthert [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top