Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6141353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:14:22+00:00 2026-05-23T18:14:22+00:00

I have an object that receives callbacks from a boost::io_service , and for a

  • 0

I have an object that receives callbacks from a boost::io_service, and for a few reasons I cannot post the callback via a shared pointer (yes, I know it’s the official way to handle it), so I bind the handler with a raw pointer. Assume it’s a fixed requirement in this situation.

Now, if I delete the object, it will, of course, still receive a callback on an outstanding socket operation with “operation aborted” error code.

The question: is there a way to force a synchronous completion of all operations when I delete an object together with its owned Asio objects (sockets, timers)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:14:22+00:00Added an answer on May 23, 2026 at 6:14 pm

    You can’t; the information has been lost at that point. You can’t even compare the function objects for equality, let alone peek inside and compare some pointer and then decide what to do.

    So, the question is: why can’t you use a shared pointer?

    The way to do it is to use shared pointers and weak pointers. If you don’t want to use shared pointers and weak pointers, you can implement the underlying mechanisms yourself. But it is generally more reliable just to use the library implementations.

    So, use a weak pointer in the callback, have the callback take a weak_ptr as an argument, call wp.lock(), check it, and if it is still valid, then dereference it. There will still be a race condition where you clear the main shared_ptr and another thread calling wp.lock() (assuming you have multiple threads), but you can resolve this by using a flag in the object.

    Update with response to comment:

    Asio is not forcing you to use a shared_ptr/weak_ptr combination. You are free to build your own solution, but you have to address the same issues.

    Assuming you can’t use a weak_ptr, you should delete the object once you are sure nothing else will use the pointer. In principle you have two basic ways of doing this:

    1. Detecting that the object has been deleted by using some additional data structure. This is what shared_ptr/weak_ptr do internally, and you are free to build your own equivalent.

    2. Wait for everything to complete and then delete the object. No requirement to use shared_ptr/weak_ptr, but you need to do the book keeping somehow.

    In these cases you end up keeping track of what’s outstanding either by hand or using a library. The basic task is the same, but you’re not forced to use a library. You are forced to solve that general problem.

    The approach you are asking for, synchronously “cancelling” every outstanding operation so that you can safely delete an object, reduces to one of these cases.

    Consider:

    class Obj {
        void queue() { wait_for_io(this, bind(&Obj::io_done, this, _1)); }
    
        void io_done(error_code const& error)
        {
            // Do stuff.
        }
    };
    
    void kill_object(Obj* o)
    {
        cancel_outstanding_operations_for_obj(o);
        delete o;
    }
    

    What does the call to cancel_outstanding_operations() do if the call to Obj::io_done() is in progress in another thread? Does it wait for it to return, or does it return immediately because the I/O operation is complete? In the “return immediately” case, the “delete o” statement is not safe. In the “wait for it to return” case, you have the “wait for everything to complete” case above, except that you’ve added a bunch of implementation complexity and you have to do the wait synchronously.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that can build itself from an XML string, and write
I have an application that receives information from a database, and is used to
I have a NSURLConnection that receives data output from a url pointing to a
My application receives strings that represent objects. I have to parse the strings to
I have an object that is mapped to a cookie as a serialized base-64
I have an object that implements IDisposable that is registered with the Windsor Container
I have an object that needs a test if the object data is valid.
I have an object that starts a thread, opens a file, and waits for
I have an object that implements ArrayAccess , Iterator and Countable . That produces
I have an object that has a list of abstract 'aninamls'. i.e. var animals

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.