Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

If Not [objectName] is nothing then [objectName].Dispose() IS NOT EVALUATED CORRECTLY

0 views
Skip to first unread message

Eric Newton

unread,
Nov 5, 2003, 9:15:33 AM11/5/03
to
Ok, heres the scenario, this is code that you'll see peppered in most of my
classes that utilize any kind of object that has a Dispose method

Dim conn as SqlConnection = MakeSqlConnection()
Try
' do some DB work
conn.Close()
Finally
' following line FAILS if conn is actually nothing
if not conn is Nothing then conn.Dispose()
' this works for some reason
if not conn is Nothing then
conn.Dispose()
end if
End try

---------------------------------

Private Shared Sub CropBitmap(ByVal sourceBitmap As System.Drawing.Bitmap,
ByVal destFilePath As String, ByVal region As System.Drawing.RectangleF)

Dim bitmap As System.Drawing.Bitmap
Dim croppedBitmap As System.Drawing.Bitmap
Dim hRes As Single = bitmap.HorizontalResolution
Dim vRes As Single = bitmap.VerticalResolution
Dim cropRegion As New System.Drawing.RectangleF(region.X * hRes, region.Y
* vRes, region.Width * hRes, region.Height * vRes)

Try
bitmap = sourceBitmap

croppedBitmap = bitmap.Clone(System.Drawing.Rectangle.Round(cropRegion),
Drawing.Imaging.PixelFormat.Format1bppIndexed)
croppedBitmap.Save(destFilePath, System.Drawing.Imaging.ImageFormat.Tiff)
Finally
' ~~~~~~ this FAILS if croppedBitmap is actually nothing:
If Not croppedBitmap is Nothing then croppedBitmap.Dispose()
' ~~~~~~ this WORKS:
If Not croppedBitmap Is Nothing Then
croppedBitmap.Dispose()
End If
End Try

End Sub

--------------------------------

My hypothesis is that the VB compiler is getting into a slightly invalid
state once its hitting the Finally, and writing out incorrect IL code.

-------------------

Eric Newton
C#/ASP developer
er...@cc.ensoft-software.com [remove the first "cc."]

Herfried K. Wagner [MVP]

unread,
Nov 5, 2003, 1:43:02 PM11/5/03
to
* "Eric Newton" <ene...@aptic.com.nospam> scripsit:

Have a look at what's compiled using "isdasm.exe".

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>

Eric Newton

unread,
Nov 6, 2003, 9:37:06 AM11/6/03
to
Ok I figured something out: it appears to be a debugger stepping anomoly...

Run the VB code and step through the Test 2 block and inline IFs, you'll
notice the debugger steps over to the inline IFs true condition but a NOP is
actually run.

--
Eric Newton
C#/ASP Application Developer
er...@cc.ensoft-software.com [remove the first "CC."]

"Eric Newton" <er...@cc.ensoft-software.com> wrote in message
news:OhcGOKHp...@TK2MSFTNGP12.phx.gbl...
> already did that, apparently its a problem maybe with the debugger? the
IL
> code looks correct in both circumstances...
>
>
> --
> Eric Newton
> C#/ASP Application Developer
> er...@cc.ensoft-software.com [remove the first "CC."]
>
> "Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message
> news:bobgch$1bt8vl$1...@ID-208219.news.uni-berlin.de...

Armin Zingler

unread,
Nov 6, 2003, 10:00:51 AM11/6/03
to
"Eric Newton" <er...@cc.ensoft-software.com> schrieb

> Ok I figured something out: it appears to be a debugger stepping
> anomoly...
>
> Run the VB code and step through the Test 2 block and inline IFs,
> you'll notice the debugger steps over to the inline IFs true
> condition but a NOP is actually run.


Right, that's a "known" feature.

--
Armin

http://learn.to/quote
http://www.plig.net/nnq/nquote.html

Herfried K. Wagner [MVP]

unread,
Nov 6, 2003, 1:53:06 PM11/6/03
to
* "Eric Newton" <er...@cc.ensoft-software.com> scripsit:

> Ok I figured something out: it appears to be a debugger stepping anomoly...
>
> Run the VB code and step through the Test 2 block and inline IFs, you'll
> notice the debugger steps over to the inline IFs true condition but a NOP is
> actually run.

Thanks for sharing the results of your research on this topic!

Eric Newton

unread,
Nov 6, 2003, 3:44:22 PM11/6/03
to
Heh, a known feature seems to be a known bug

"Its not a bug... its a feature" [tongue in cheek]


--
Eric Newton
APTItude Solutions
ene...@aptic.com.nospam

"Armin Zingler" <az.n...@freenet.de> wrote in message
news:uBnYdgHp...@TK2MSFTNGP12.phx.gbl...

0 new messages