This is probably going to get a responses that say "use good scoping" but
I'll ask it anyway because VB6 didn't really have a special garbage collector and
I'm curious about getting feedback in regard to how "good" (real super
smart and efficient) garbage collection
should work.
(..and I know that no GC can never be 100% efficient but..)
Let's say I'm a noob VB.Net programmer
(and
some days I feel more of a newbie VB.Net programmer than others,
but that's besides the point..).
I need to load a bitmap and do some manipulation on it.
My fantastic code looks like:
Code:
Public Class Form1
Private m_Bitmap As Bitmap
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim strFilename As String = "C:\superhuge_2560px_by_2048px_32bpp.bmp"
m_Bitmap = New Bitmap(strFilename)
BadBitmapScoping()
End Sub
Private Sub BadBitmapScoping()
Dim bm As Bitmap = Nothing
bm = m_Bitmap
'perform some bitmap manipulation here..
'
'
End Sub
End Class
Of course the experts on the forum are just shaking their collective heads
going "tsk, tsk, tsk" at the many errors.
Some will point out that it's m,ore "polite" to not lock up the bitmap file this way,
but instead use code to "
Load a picture so it doesn't lock the picture file in VB .NET".
Others will point out (ask):
"Shouldn't that be a function instead of a sub so it returns
the manipulated bitmap for use in other parts of the program?".
But the most glaring error is the bad scoping - setting up a module level variable that
gets assigned to a locally scoped variable is a bad practice in this case,
when simply using a local variable would gave been better
(and would have helped the garbage collector,
because the bitmap, after being loaded, isn't need/used anymore after
the manipulated bitmap is created).
How can/should the .Net GC handle this situation?
(and/or what is the degree that the GC can/should compensate for this bad coding?)
Because of the module level assignment,
it basically means our superhuge bitmap
(which should be a png if its going to use 32bpp alpha, but anyways..),
is going to be hanging out in memory until the program ends
and/or possibly if the form goes out of scope.
A secondary question - should the IDE be raising some kind of warning about this?
I tried this code out in VB.Net 2010 Express Edition and no error or warning is raised (so the IDE auto-correct had no suggestions..)
If the .Net IDE was smart enough to detect this bad scoping,
what
should it do about it?
I have a bad feeling it would never be smart enough to
directly correct the bad scoping code.
Instead I think if it did detect it,
it's auto-correct suggestion it might be to
add a bm.Dispose() statement:
Quote:
Private Sub BadBitmapScoping()
Dim bm As Bitmap = Nothing
bm = m_Bitmap
'perform some bitmap manipulation here..
'
bm.Dispose()
End Sub
|
..which would of course do nothing about the module level scoped bitmap.
The other option might be for the compiler to somehow do something to compensate for the bad scoping,
but I don't know enough about compiler technology to know about what could be done at that level.