|
Boost Users : |
Subject: Re: [Boost-users] [ASIO] random crashes
From: Bill Somerville (bill_at_[hidden])
Date: 2009-01-30 05:37:19
Axel wrote:
> Jon Biggar wrote:
>>
>>
>> From my experience, this is likely due to trying to invoke a member
>> function on a lock that is already destroyed. You probably have a
>> race condition where you are doing something on the ASIO socket after
>> it's already been destroyed.
>
> The only thread who can destroy the instance of the object (the asio
> socket instance is a member of this object) is the io_service running
> thread.
>
> The destroy instruction is in a receive error handler : I cancel
> timer, close the socket and destroy the instance ; could the timeout
> handler (which seems to cause the crash) be executed after the
> destruction of the object ?
>
> Maybe my design is bad, what would be a proper way to handle socket
> timeout and socket explicit close ?
>
> My design looks like :
>
> explicit_close()
> {
> timer.cancel() ; // triggers handle_timeout
> socket.cancel() ;
> }
I recall that deadline_timer.cancel() is not thread safe, if this
explicit_close() is being called in a different thread from the
io_service thread then I believe there is a race condition if the timer
is active while being cancelled. I may be wrong as I am a little rusty
on boost asio.
>
> handle_timeout()
> {
> socket.close() ; // this triggers the handle_receive() function
> }
>
> handle_receive()
> {
> if(! error) {}
> else{timer.cancel() ; socket.close() ; delete this ;}
> }
HTH
Bill Somerville
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net