473,499 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(cropRe gion),
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."]

Nov 20 '05 #1
5 3641
* "Eric Newton" <en*****@aptic.com.nospam> scripsit:
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(cropRe gion),
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.


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>
Nov 20 '05 #2
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:Oh**************@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]" <hi***************@gmx.at> wrote in message
news:bo*************@ID-208219.news.uni-berlin.de...
* "Eric Newton" <en*****@aptic.com.nospam> scripsit:
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(cropRe gion), 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.


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>


Nov 20 '05 #3
"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

Nov 20 '05 #4
* "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!

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

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #5
Heh, a known feature seems to be a known bug

"Its not a bug... its a feature" [tongue in cheek]
--
Eric Newton
APTItude Solutions
en*****@aptic.com.nospam

"Armin Zingler" <az*******@freenet.de> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
"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

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6015
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
1
1330
by: JP | last post by:
How do you dispose of an user defined external class (CS file) after youve finished using it? Ive tried IDispose but i feel I may not be using it correctly. -- JP ..NET Software Develper
5
1645
by: DraguVaso | last post by:
Hi, I have made some classes, but when they are Finalized/Disposed or simply "MyClass = Nothing", I want to trigger this action, and ask to save unsaved changes. How should I do this? I'm...
11
5258
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
16
4224
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object out of principle?
3
6332
by: LP | last post by:
Hi, Considering the following code: using(mySQLconn) { if (someCondition==true) return 0; //more code after return //do some db calls
156
5729
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
9
4517
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
34
2938
by: =?Utf-8?B?SGFvIEppYW5n?= | last post by:
Hi, I see in many MS example code that looks like this Public Sub Foo() dim myCustomer as New Customer ... myCustomer.Dispose() myCustomer = Nothing End Sub
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.