Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb_dotnet thread: am i actually releasing resources?


Message #1 by "Abraham Luna" <abe@a...> on Wed, 20 Sep 2000 15:21:40 -0400
See comments.  Hope the comments help you.  Garbage collection is both
wonderful and strange coming from VB6.  Remember you only control what the
object "references" are pointed at -- not the existence  of the objects
themselves after your reference is removed.  If the garbage collector finds
nothing referencing an object, then that object is placed on a list of
objects to be removed from memory on a 2nd pass of the garbage collector.
What we programmers have responsibility to do is to be certain that the
objects we release do not have other resources opened (such as files,
printers and database connections), the rest is automatic as soon as we and
the garbage collector jointly remove all references to an object.

----- Original Message -----
From: "Abraham Luna" <abe@a...>
To: "pro_VB_dotnet" <pro_vb_dotnet@p...>
Sent: Wednesday, September 20, 2000 12:21 PM
Subject: [pro_vb_dotnet] am i actually releasing resources?


> just wanted to know if my code is actually releasing resources:
>
> Dim ofdFileName As New OpenFileDialog()
>
> If ofdFileName.ShowDialog = DialogResult.OK Then
>
> Dim strFileName As String = ofdFileName.FileName
>
> Dim bmpBitmap As New Bitmap(strFileName)
>
> Dim imgImage As Image = CType(bmpBitmap, Image)
>
> frmNewDocument = New NewDocument()
>
> frmNewDocument.PictureBox1.Image = imgImage
>
> frmNewDocument.Width = imgImage.Width + 6
>
> frmNewDocument.Height = imgImage.Height + 26
>
> frmNewDocument.PictureBox1.Left = 0
>
> frmNewDocument.PictureBox1.Top = 0
>
> frmNewDocument.Text = strFileName
>
> frmNewDocument.MdiParent = Me
>
> frmNewDocument.Show()
>
> frmNewDocument = Nothing    <==== This sets the pointer to null,
frmNewDocument now has a life independent of the "me" form except that in a
MIDI application, the MidiParent will be the last module to be released (the
reference to the MidiParent keeps the MidiParent around).
>
> imgImage = Nothing   <=== since it is likely no other object is pointed to
this, imgImage object is ready for garbage collection after this line.  The
pointer, imgImage,  points to Nothing immediately after this line even if
the imgImage's referenced object is still hanging around in RAM waiting for
garbage collection.
>
> bmpBitmap = Nothing  <==== ditto
>
> strFileName = Nothing  <=== ditto
>
> End If
>
> ofdFileName.Dispose()  <== this is a request to the Open File Dialog
object to call its destructor function.  What happens next depends entirely
on how the destructor is written.  Probably, it prepares the object for
eventual garbage collection.  The ofdFileName pointer will point to nothing
soon after this line of code (but not immediately -- even if you request
garbage collection next, that only means you are requesting the garbage
collector to give this item higher priority to be collected....).
>
> ---------
> i'm using visual studio .net on windows 2000 pro
>
>
> ---
> Visual Basic .NET Text Manipulation Handbook
> The .NET Framework brings a variety of string manipulation features
> to the VB language, and some of these, namely regular expressions
> and the StringBuilder class, are something VB 6 developers may not
> have seen before. This book teaches you how to manipulate text using
> these string matching, manipulation, and replacement classes. Issues
> such as Localization and data conversion will also be investigated.
> http://www.wrox.com/ACON11.asp?ISBN=1861007302


  Return to Index