[Boost-users] Asio io_service::work destructor throwing mutex exception

95 views
Skip to first unread message

Florian Turck

unread,
Dec 12, 2009, 5:48:57 PM12/12/09
to boost...@lists.boost.org
Hi,

I am using asio´s io_service::work class to prevent io_service.run()
from returning. Unfortunatly on deletion of my object the destructor of
io_service::work throws an exception:

terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error>
>'
what(): mutex: Invalid argument

Thread 0 Crashed: Dispatch queue: com.apple.main-thread
0 libSystem.B.dylib 0x9578c732 __kill + 10
1 libSystem.B.dylib 0x9578c724 kill$UNIX2003 + 32
2 libSystem.B.dylib 0x9581f98d raise + 26
3 libSystem.B.dylib 0x95835a44 abort + 93
4 libstdc++.6.dylib 0x9122afda
__gnu_cxx::__verbose_terminate_handler() + 433
5 libstdc++.6.dylib 0x9122917a
__cxxabiv1::__terminate(void (*)()) + 10
6 libstdc++.6.dylib 0x9122851d __cxa_call_terminate + 53
7 libstdc++.6.dylib 0x9122909d __gxx_personality_v0 + 911
8 libSystem.B.dylib 0x9579aa9d unwind_phase2 + 205
9 libSystem.B.dylib 0x957b1fd9 _Unwind_Resume + 73
10 com.yourcompany.ReclaimR 0x00026ed3
boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>::scoped_lock(boost::asio::detail::posix_mutex&)
+ 79 (scoped_lock.hpp:37)
11 com.yourcompany.ReclaimR 0x00027dfa
boost::asio::detail::task_io_service<boost::asio::detail::kqueue_reactor<false>
>::work_finished() + 28 (task_io_service.hpp:171)
12 com.yourcompany.ReclaimR 0x00027e72
boost::asio::io_service::work::~work() + 22 (io_service.ipp:155)

The asio related objects are all members of my class:

[...]
private:

io_service ioService;
io_service::work ioWork;
thread ioThread;
tcp::socket controlSocket;
tcp::acceptor acceptor;
[...]

This is how I initialize everything:

[...]
MyClass() : ioService(),
ioWork(ioService),
ioThread(bind(&io_service::run, &ioService)),
controlSocket(ioService),
acceptor(ioService),
[...]

Finally this is what happens in the destructor:

[...]
if (connected)
disconnect();
ioService.stop();
ioThread.join();
[...]

Anyone knows how to deal with this?

Regards,

Florian
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Boris Schaeling

unread,
Dec 13, 2009, 9:21:41 AM12/13/09
to boost...@lists.boost.org
On Sat, 12 Dec 2009 23:48:57 +0100, Florian Turck <ma...@florianturck.de>
wrote:


> I am using asio´s io_service::work class to prevent io_service.run()
> from returning. Unfortunatly on deletion of my object the destructor of
> io_service::work throws an exception:
>
> terminate called after throwing an instance of
> 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error>
>> '
> what(): mutex: Invalid argument

I always use a dynamically allocated work object and store it in a
scoped_ptr. This works fine (I've changed your code below). However I
don't remember having read in the Boost.Asio documentation that your
approach is wrong. But I don't remember either where I got this idea with
the dynamically allocated object from.

> [...]
> private:
>
> io_service ioService;
> io_service::work ioWork;

boost::scoped_ptr<io_service::work> ioWork;

> [...]
> MyClass() : ioService(),
> ioWork(ioService),

ioWork(new io_service::work(ioService)),

> [...]
> if (connected)
> disconnect();

ioWork.reset();

> ioService.stop();
> ioThread.join();
> [...]

HTH,
Boris

Reply all
Reply to author
Forward
0 new messages