[rabbitmq-discuss] difficulty closing connections in Windows Forms apps

Hubert Plociniczak hubert at lshift.net
Tue Aug 12 14:39:08 BST 2008


Hi Geoffrey,


there is a high probability that your problem was caused by the problem

described in:

http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/2008-August/001548.html

Using  Windows Forms application shouldn't really matter.


Geoffrey Anderson wrote:
> 3. Scenario B (the Hard place):  When I use connection.AutoClose = False style of
> connection management, I added a line of code which executes at FormClose event
> which simply does connection.Close().   In this case your .net library throws a
> socket error saying that the connection was already destructed and is not a viable
> object to be calling Close() on.  (I am paraphrasing, it's not exactly the text.) 
> Hmmm...  OK I try one more thing:   I do this to make sure it's open before I close
> it, that should prevent some problems, right?  No.  
>
>  If (conn.IsOpen)
>     conn.Close()
>   
Actually, the above pattern should not normally be used because of possible race
conditions (see documentation on more info).
So generally you should only perform the operation without checking if the connection
is closed. This will work correctly when the problem mentioned at the beginning is
addressed. 

> Well here's the funny thing.  The conn.IsOpen returns true, code execution moves to
> the next line, and the conn.Close() doesn't even run.... it throws the error
> indicating the connection object was already destructed.   Which is quite odd
> because just a moment ago, one line of code earlier, the object was not destructed.
>   
It appears that the bug itself was during performing Close operation, hence the
connection was 'correctly' in open state before you tried to close it.

> Am I supposed to avoid calling connection.Close() and just call Dispose()???  I
> would think Close is there for a reason....  and that I should be able to call
> Close() in some scenarios.  I know that the 'using' statement calls Dispose
> implicitly in C#....
>   
You were right. Calling connection.Close() is the right way.

Thanks for reporting the problem!

Hubert

-- 
 [][][] Hubert Plociniczak
   [][] LShift Ltd
 []  [] www.lshift.net





More information about the rabbitmq-discuss mailing list