PC Review


Reply
Thread Tools Rate Thread

Calling Dispose() after ShowDialog()

 
 
Steve Austin
Guest
Posts: n/a
 
      24th Jun 2004
Is this absolutely necessary to avoid memory leaks? If so, would:

using( MyForm form = new MyForm() )
{
form.ShowDialog();
}

be acceptable?

Steve


 
Reply With Quote
 
 
 
 
Stoitcho Goutsev \(100\) [C# MVP]
Guest
Posts: n/a
 
      24th Jun 2004
Hi Steve,

Yes it is completely fine if you do all dialog box's field reading inside
the using block.

Just a little correction. You won't have memory leaks even if you don't call
dispose. As long as you don't keep references to the dialog box it will be
garbage collected and disposed as soon as there is need of memory. But
because the dialog box holds unamaged resources it is good practice to call
dispose ASAP when you finish using it. Bare in mind that GC doesn't monitor
unmanaged resources and GC will not collect the garabage if you run out of
unmanaged resources or memory. In the next version of the framework GC
introduces one new method (AddMemoryPressure) that is kind of workaround
those problems

--
HTH
Stoitcho Goutsev (100) [C# MVP]


"Steve Austin" <(E-Mail Removed)> wrote in message
news:huOdneb5c-dzVkfdRVn-(E-Mail Removed)...
> Is this absolutely necessary to avoid memory leaks? If so, would:
>
> using( MyForm form = new MyForm() )
> {
> form.ShowDialog();
> }
>
> be acceptable?
>
> Steve
>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      24th Jun 2004
* "Steve Austin" <(E-Mail Removed)> scripsit:
> Is this absolutely necessary to avoid memory leaks? If so, would:
>
> using( MyForm form = new MyForm() )
> {
> form.ShowDialog();
> }
>
> be acceptable?


In addition to the other post: Call 'Dispose' for forms shown using
'ShowDialog', don't call it for forms shown using 'Show'. These forms
will be disposed automatically when they are closed.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
 
Reply With Quote
 
Steve Austin
Guest
Posts: n/a
 
      24th Jun 2004
Thanks for clarifying that fellas

"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>* "Steve Austin" <(E-Mail Removed)> scripsit:
>> Is this absolutely necessary to avoid memory leaks? If so, would:
>>
>> using( MyForm form = new MyForm() )
>> {
>> form.ShowDialog();
>> }
>>
>> be acceptable?

>
> In addition to the other post: Call 'Dispose' for forms shown using
> 'ShowDialog', don't call it for forms shown using 'Show'. These forms
> will be disposed automatically when they are closed.
>
> --
> Herfried K. Wagner [MVP]
> <URL:http://dotnet.mvps.org/>



 
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
Tooltip, .ShowDialog(), and .Dispose() on .NET 2.0 generate a NullReferenceException Emmanuel_ Microsoft Dot NET Framework Forms 3 4th Jun 2006 09:13 PM
ApplicationEx.ShowDialog and Form.Dispose Myron Marston Microsoft Dot NET Compact Framework 1 20th Jul 2005 11:48 PM
ShowDialog() =?= Dispose()? Phill. W Microsoft VB .NET 1 1st Jul 2004 05:09 PM
does ShowDialog automatically dispose? cody Microsoft Dot NET Framework Forms 1 8th Dec 2003 04:29 PM
Calling ShowDialog on MouseDown event fhunter Microsoft Dot NET Compact Framework 1 30th Sep 2003 10:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 PM.