Hello,

I've written the following little helper function which calls Dispose on
classes which implement the idisposable interface.

Public Sub DisposeObject(ByVal DisposeAbleObject As IDisposable)
If Not DisposeAbleObject Is Nothing Then
DisposeAbleObject.Dispose()
End If
End Sub

The reason for this is to avoid ugly looking finally clauses with lots
of checking if an object ref is nothing before calling dispose on them.
For example in a try finally block in which a connection is opnened, a
transaction is started and an sqldatareader gets created which looks
like this:

try
.....
finally
if sqlconnection is nothing then
sqlconnection.close
end if

if Transaction is nothing then
Transaction.dispose
end if

if Datareader is nothing then
Datareader.close
end if
end try

i'd write this:

try
....
finally
disposeobject(sqlconnection)
disposeobject(transaction)
disposeobject(sqldatareader)
end try

a lot better looking IMO. I dont have to worry about whether the ref is
nothing or not.

Now my question is this: Is this going to cause any strange behavior?
I've tested it and it seems to work.. but im wondering if there could be
any side effects since im not calling the specific Close of the
datareader or sqlconnection, im calling the Dispose. The documentation
says there's not much difference in calling Dispose instead of Close on
a connection, but can i assume the same for any object that implements
IDisposable?

Thanks

RE: Calling Dispose instead of close, clear, .... by NoSpamMgbworld

NoSpamMgbworld
Mon Sep 27 07:43:01 CDT 2004

I am obviously missing something here. Not sure why the is nothing in here
with data objects. Normally, the pattern is something like this:

Try
conn.Open()
'Work with conn here
Finally
If conn.State = ConnectionState.Open Then
conn.Close()
End If

conn.Dispose()
End Try

In this model, if I were simply to test for conn is nothing in the finally,
it would never dispose.

The other issue is you are not always closing. Dispose without a close is
not a wise choice. If the object is not cleaned up, you could still be
holding on to resources until the GC actually cleans it up. You have called
Dispose(), which is good, but the memory could be held for a time despite
Disposing. If the memory location is hooked to an expensive resource, like a
connection, you could have that resource tied up for a bit, especially on a
machine with lots of RAM.

As for the idea of passing an object off to a routine and having it work on
the interface, I see nothing wrong with that.

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************


"Joop" wrote:

> Hello,
>
> I've written the following little helper function which calls Dispose on
> classes which implement the idisposable interface.
>
> Public Sub DisposeObject(ByVal DisposeAbleObject As IDisposable)
> If Not DisposeAbleObject Is Nothing Then
> DisposeAbleObject.Dispose()
> End If
> End Sub
>
> The reason for this is to avoid ugly looking finally clauses with lots
> of checking if an object ref is nothing before calling dispose on them.
> For example in a try finally block in which a connection is opnened, a
> transaction is started and an sqldatareader gets created which looks
> like this:
>
> try
> ......
> finally
> if sqlconnection is nothing then
> sqlconnection.close
> end if
>
> if Transaction is nothing then
> Transaction.dispose
> end if
>
> if Datareader is nothing then
> Datareader.close
> end if
> end try
>
> i'd write this:
>
> try
> .....
> finally
> disposeobject(sqlconnection)
> disposeobject(transaction)
> disposeobject(sqldatareader)
> end try
>
> a lot better looking IMO. I dont have to worry about whether the ref is
> nothing or not.
>
> Now my question is this: Is this going to cause any strange behavior?
> I've tested it and it seems to work.. but im wondering if there could be
> any side effects since im not calling the specific Close of the
> datareader or sqlconnection, im calling the Dispose. The documentation
> says there's not much difference in calling Dispose instead of Close on
> a connection, but can i assume the same for any object that implements
> IDisposable?
>
> Thanks
>
>
>

RE: Calling Dispose instead of close, clear, .... by Jon

Jon
Mon Sep 27 08:30:55 CDT 2004

Cowboy (Gregory A. Beamer) - MVP <NoSpamMgbworld@comcast.netNoSpamM>
wrote:
> I am obviously missing something here. Not sure why the is nothing in here
> with data objects. Normally, the pattern is something like this:
>
> Try
> conn.Open()
> 'Work with conn here
> Finally
> If conn.State = ConnectionState.Open Then
> conn.Close()
> End If
>
> conn.Dispose()
> End Try
>
> In this model, if I were simply to test for conn is nothing in the finally,
> it would never dispose.

Why? There's nothing in the above which sets conn to nothing in the
above, as far as I can see.

> The other issue is you are not always closing. Dispose without a close is
> not a wise choice.

Dispose without Close is absolutely fine. What do you think the problem
is?

> If the object is not cleaned up, you could still be
> holding on to resources until the GC actually cleans it up. You have called
> Dispose(), which is good, but the memory could be held for a time despite
> Disposing. If the memory location is hooked to an expensive resource, like a
> connection, you could have that resource tied up for a bit, especially on a
> machine with lots of RAM.

If you've called Dispose, the object *won't* be hooked to an expensive
resource, assuming the Dispose method is written properly.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too