Boost logo

Boost :

Subject: [boost] FW: deadline_timer and a crash at ~io_service -> timer_base::destroy()
From: Vance Grkov (vanceg_at_[hidden])
Date: 2010-04-13 13:22:14


Hi,

I am getting a crash at the scope end (before the "catch") below in the code. This is the stack trace and the source. Does anyone know what is the problem with this code? Appreciate your help!

     000801c7()
> BoostThreadPoolTest.exe!boost::asio::detail::timer_queue<boost::asio::time_traits<boost::posix_time::ptime> >::timer_base::destroy() Line 211 + 0xe bytes C++
     BoostThreadPoolTest.exe!boost::asio::detail::timer_queue<boost::asio::time_traits<boost::posix_time::ptime> >::destroy_timer_list(boost::asio::detail::timer_queue<boost::asio::time_traits<boost::posix_time::ptime> >::timer_base * & t=0x00338da0) Line 415 C++
     BoostThreadPoolTest.exe!boost::asio::detail::timer_queue<boost::asio::time_traits<boost::posix_time::ptime> >::destroy_timers() Line 189 C++
     BoostThreadPoolTest.exe!boost::asio::detail::win_iocp_io_service::shutdown_service() Line 148 + 0x2c bytes C++
     BoostThreadPoolTest.exe!boost::asio::detail::service_registry::~service_registry() Line 75 + 0xf bytes C++
     BoostThreadPoolTest.exe!boost::asio::detail::service_registry::`scalar deleting destructor'() + 0x2b bytes C++
     BoostThreadPoolTest.exe!boost::asio::io_service::~io_service() Line 52 + 0x2e bytes C++
     BoostThreadPoolTest.exe!wmain(int argc=1, wchar_t * * argv=0x003371d8) Line 152 + 0x27 bytes C++
     BoostThreadPoolTest.exe!__tmainCRTStartup() Line 583 + 0x19 bytes C
     BoostThreadPoolTest.exe!wmainCRTStartup() Line 403 C
     kernel32.dll!_BaseProcessStart_at_4() + 0x23 bytes

#include "stdafx.h"

#include <iostream>

#include <boost/thread.hpp>

#include <boost/bind.hpp>

#include <boost/shared_ptr.hpp>

#include <boost/asio.hpp>

 

#define
THREAD_POOL_SIZE 10

 

void
TimerCallback(boost::system::error_code ec, std::size_t i, std::size_t retry) {

      std::stringstream tmp;

      tmp << "Timer("
<< ec.value() << ":"
<< ec.message() << "-"
<< retry << "-"
<< i << ")\n";

}

 

//

// MAIN

//

 

int _tmain(int argc, _TCHAR* argv[])

{

      try

      {

            boost::asio::io_service
io(THREAD_POOL_SIZE);

            boost::asio::io_service::work
*moreWorkToCome = new
boost::asio::io_service::work(io);

 

            // Create
a pool of worker threads. std::vector<boost::shared_ptr<boost::thread>
> threads;

            for
(std::size_t i = 0; i < THREAD_POOL_SIZE; ++i)

            {

                  boost::shared_ptr<boost::thread>
thread(new boost::thread(

                        boost::bind(&boost::asio::io_service::run,
&io)

                        ));

 

                  threads.push_back(thread);

            }

            

            // Lets
enqueue more tasks to run after a given time

            std::vector<boost::asio::deadline_timer
*> timers;

            for
(std::size_t i = 1; i<50; i++)

            { boost::asio::deadline_timer
*timer = new
boost::asio::deadline_timer(io);

                  timer->expires_from_now(boost::posix_time::seconds(3));

                  timer->async_wait(boost::bind(&TimerCallback,
boost::asio::placeholders::error, i, 0));

                  timers.push_back(timer);

            }

 

            std::cout << "\nALL TASKS QUEUED\n";

 

            // Now
that all tasks have been queued we don't want to hold on any of the worker
threads delete
moreWorkToCome;

 

            std::cout << "\nRELEASE THE THREAD POOL\n";

             // Wait
for all tasks to be processes which also means all threads in the pool will
exit. for
(std::size_t i = 0; i < threads.size(); ++i)

                  threads[i]->join();

 

            // Cancel
all timers (just in case)

            for
(std::size_t i = 0; i < timers.size(); ++i)

                  timers[i]->cancel();

 

            io.stop();

 

            std::cout << "\nPOOL DOWN\n";

      } <-------------------------------------------- CRASH

      catch(...)

      {

            std::cout << "ERROR";

      }

      

      std::cout << "\nEXIT\n";

 

      return 0;

}

 

 

                                               
Hotmail: Trusted email with powerful SPAM protection. Sign up now.
_________________________________________________________________
Hotmail: Free, trusted and rich email service.
https://signup.live.com/signup.aspx?id=60969


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk