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 5841503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:53:55+00:00 2026-05-22T11:53:55+00:00

Lets say I have heap allocated A* , which I want to pass as

  • 0

Lets say I have heap allocated A*, which I want to pass as argument to boost::bind.
boost::bind is saved for later processing in some STL like container of boost::functions‘s.

I want to ensure A* will be destroyed at destruction of the STL container.

To demostrate:

A* pA = new A();

// some time later
container.push_back(boost::bind(&SomeClass::HandleA, this, pA);

// some time later
container is destroyed => pA is destroyed too

How can it be done?

EDIT

Maybe what I want is not that realistic.

I have raw pointer and function which receives the raw pointer. The call is delayed by means of boost::bind. At this point I want automatic memory management in case boost::bind want executed. I’m lazy, so I want to use “ready” smart-pointer solution.

std::auto_ptr looks like a good candidate, however …

auto_ptr<A> pAutoA(pA);
container.push_back(boost::bind(&SomeClass::HandleA, this, pAutoA);

doesn’t compile (see here)

auto_ptr<A> pAutoA(pA);
container.push_back(boost::bind(&SomeClass::HandleA, this, boost::ref(pAutoA));

pAutoA is destroyed, deleting underlying pA.

EDIT 02

In the mentioned container I will need to store misc “callbacks” with different arguments. Some of them are raw pointers to object. Since the code is old, I not always can change it.

Writing own wrapper for storing callbacks in container is last resort (while maybe the only one), hence bounty.

  • 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-22T11:53:56+00:00Added an answer on May 22, 2026 at 11:53 am

    The idea of @pmjordan was already going in the right direction. You replied that you can’t use shared_ptr, because you can’t take ownership back from it once constructed. But that is not entirely correct: with shared_ptr‘s custom deleter mechanism, you can. This is how:

    Assume these toy defintions for your A and f(A*):

    struct A {
        ~A() { std::cout << "~A()" << std::endl; }
    };
    
    void f( A * a ) {
        std::cout << "in f(A*)" << std::endl;
        delete a;
    }
    
    1. Write a deleter that can be “switched off”:

      struct opt_delete {
          bool m_delete;
          opt_delete() : m_delete( true ) {}
          template <typename T>
          void operator()( T * t ) {
              if ( m_delete ) delete t;
          }
      };
      
    2. Then you can write a take() function that takes ownership of the shared_ptr payload again:

      template <typename T>
      T * take( const boost::shared_ptr<T> & sp ) {
          opt_delete * d = boost::get_deleter<opt_delete>( sp );
          assert( d );
          assert( d->m_delete == true );
          d->m_delete = false;
          return sp.get();
      }
      

      (this will leave the payload in the remaining shared_ptr instances, but for your case, that’s ok, and the assert()s cover the cases when it’s not).

    3. Now you can manually wrap f(A*) like this:

      void f_sp( const boost::shared_ptr<A> & a ) {
          f( take( a ) );
      }
      
    4. And finally, test the two scenarios:

      int main( int argc, char * argv[] ) {
      
          const boost::shared_ptr<A> a( new A, opt_delete() );
      
          const boost::function<void()> func =
              boost::bind( &f_sp, a );
      
          if ( argc >= 2 && *argv[1] == '1' ) // call 'func'
              func();
          else
              ; // don't
      
          return 0;
      }
      

    Executing the test program with a 1 argument will print

    in f(A*)
    ~A()

    and without (or any other argument), it will print

    ~A()

    You can extend the test harness to put func into a container first, but it’ll still be safe. The only thing that isn’t safe in the case is calling the func copies more than once (but then you’ll trigger the second assertion in take()).

    EDIT: Note that this mechanism isn’t thread-safe. To make it thread-safe, you need to supply opt_delete with a mutex to synchronise operator() with take().

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

Sidebar

Related Questions

Lets say I have multiple commands like 5000 which updates some column and row
Let's say I have 2 singletons, allocated on the heap, for which no delete
Lets say I have a .c file with some methods, and I create a
Lets say I have these columns uniqueID|Money|Quantity|MoneyOrder|QuantityOrder 1|23|12|| 2|11|9|| 3|99|100|| What I want to
Lets say we have an Edit View to edit our data, and we want
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say we have a table here, populated with the following data: acc_id1 acc_id2
Lets say I have five tables named table1, table2 ... table5. I have already
Lets Say i have a table like this WEB_LIST_TABLE KEY Value ---------------------------------------- 134 google.com
Lets say i have at least two lua script files. test1.lua test2.lua both define

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.