Boost logo

Boost Users :

Subject: Re: [Boost-users] [ASIO] Access violation on io_service::work destructor
From: Daniele Barzotti (daniele.barzotti_at_[hidden])
Date: 2010-09-23 06:00:14


Il 23/09/2010 11:18, Rutger ter Borg ha scritto:
>
> Besides a possible race in your statement
>
> thread_started = !thread_started;
>
> and some other bad stuff such as memory leaks when throwing an
> exception, I don't see where the destructor of the work object is
> called. Could you provide the code involved?

Hi Rutger,

thanks for your reply.

Why ' thread_started = !thread_started; ' is a bad stuff?

Now, when I crate an object that use io_service, I assign the pointer to
io_service::work to a shared_pointer:

typedef boost::shared_ptr<boost::asio::io_service::work> io_work_ptr;

EXTERN_C DLL_EXPORT MyObjectHandle CALL GetMyObj()
{
  if (!thread_started) {
    try {
      // create the work object on the heap
      p_work = new boost::asio::io_service::work(io_service);

      // run the IO service as a separate thread:
      io_service_thread.reset(new boost::thread(io_worker_thread));

      thread_started = !thread_started;

    } catch (boost::thread_resource_error e) {
      // Failed to create the new thread
      return 0;
    }
  }
  // create the new object
  MyObject* pMyObject = new MyObject(io_service);

  // Assign a reference to io_service work object:
  // This reference is used to counting the objects created.
  // When the last MyObject is deleted the main work object
  // is destroyed and the io_service thread ends.
  pMyObject->_io_work_ptr.reset(p_work);

  // return the object
  return pMyObject;
};

Mmmm, now I'm thinking about, I have this issue only if I create more
than 1 object...

Regards,
Daniele.


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