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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:17:49+00:00 2026-05-27T02:17:49+00:00

I would like to store a reference to an object as a weak_ptr. In

  • 0

I would like to store a reference to an object as a weak_ptr. In pure C++, the following works :

#include <iostream>

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>

using namespace std;
using namespace boost;

struct Empty
{
    Empty(){}
};

struct Store
{
    weak_ptr<Empty> value;
    Store(){};

    void setValue(shared_ptr<Empty> v) {
        cout << "storing " << v << endl;
        this->value = weak_ptr<Empty>(v);
        shared_ptr<Empty> v_ok = this->value.lock();
        if (v_ok) {
            cout << "ok, v has been stored" << endl;
        }
    }

    shared_ptr<Empty> getValue() {
        shared_ptr<Empty> p = this->value.lock();
        if (p) {
            cout << "stored value : " << p << endl;
        } else {
            cout << "there's nothing here !" << endl;
        }
        return p;
    }
};

int main()
{
    shared_ptr<Empty> e(new Empty);
    shared_ptr<Store> st(new Store);

    st->setValue(e);
    st->getValue();
    return 0;
}

compiling and running this will give you this :

%> ./a.out 
storing 0x8c6c008
ok, v has been stored
stored value : 0x8c6c008

Now, if I encapsulate that with boost python :

#include <iostream>

#include <boost/shared_ptr.hpp>
#include <boost/python.hpp>
#include <boost/weak_ptr.hpp>

using namespace std;
using namespace boost;
using namespace boost::python;

struct Empty
{
    Empty(){}
};

struct Store
{
    weak_ptr<Empty> value;
    Store(){};

    void setValue(shared_ptr<Empty> v) {
        cout << "storing " << v << endl;
        this->value = weak_ptr<Empty>(v);
        shared_ptr<Empty> v_ok = this->value.lock();
        if (v_ok) {
            cout << "ok, v has been stored" << endl;
        }
    }

    shared_ptr<Empty> getValue() {
        shared_ptr<Empty> p = this->value.lock();
        if (p) {
            cout << "stored value : " << p << endl;
        } else {
            cout << "there's nothing here !" << endl;
        }
        return p;
    }
};

BOOST_PYTHON_MODULE (test)
{
    class_< Empty, shared_ptr<Empty> >("Empty");

    class_< Store, shared_ptr<Store> >("Store")
    .def("get",&Store::getValue)
    .def("set",&Store::setValue);
}

and now a small python script to try it out

from test import *

e = Empty()
st = Store()

st.set(e)
st.get()

… and the result is …

storing 0x9eb2a18
ok, v has been stored
there's nothing here !

so apparently while I’m still in the same method (setValue), there is no problem retrieving a
shared_ptr from Store::value. But as soon as I get out of this context, there’s nothing left !

How can this be ? Is python passing a brand new (and useless) shared_ptr as an argument to setValue, which is then destroyed at the end of the call ? I’m lost here.

  • 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-27T02:17:50+00:00Added an answer on May 27, 2026 at 2:17 am

    This is extremely curious. I’ve ruled out the std vs. boost shared pointer possibility, and played around with a few sanity checks, and as far as I can tell it’s something that boost python is doing to the shared pointer that breaks it.

    Tracing object constructors / destructors, the lifetimes of Empty and Store are being managed as you would expect (no copies occur).

    An extremely interesting thing is that shared_from_this continues to work, even when weak_ptr<>.lock() doesn’t, and, in fact, a new weak pointer created from a new shared pointer (from shared_from_this) does work.

    So this lead me to the thread linked in the comments, it seems like there’s something boost python is doing with the deleter and reference count that is breaking the weak pointer.

    Inspecting the shared pointers in the debugger this is what we get:

    When we call setValue, this is what the argument looks like:

    1: p = (const 'boost::shared_ptr<Empty>' &) @0x7fff5fbfe720: {
      px = 0x100346900, 
      pn = {
        pi_ = 0x100338dd0
      }
    }
    > p *p.pn.pi_
    $5 = (boost::detail::sp_counted_impl_pd<void*,boost::python::converter::shared_ptr_deleter>) {
      <boost::detail::sp_counted_base> = {
        _vptr$sp_counted_base = 0x10061aa30, 
        use_count_ = 2, 
        weak_count_ = 2
      }, 
      members of boost::detail::sp_counted_impl_pd<void*,boost::python::converter::shared_ptr_deleter>: 
      ptr = 0x0, 
      del = {
        owner = {
          m_p = 0x10049db90
        }
      }
    }
    

    If we create a shared pointer using shared_from_this on the argument, it looks like this:

    1: p = (const 'boost::shared_ptr<Empty>' &) @0x7fff5fbfe5e0: {
      px = 0x100346900, 
      pn = {
        pi_ = 0x1003468e0
      }
    }
    > p *p.pn.pi_
    $4 = (boost::detail::sp_counted_impl_pd<Empty*,boost::detail::sp_ms_deleter<Empty> >) {
      <boost::detail::sp_counted_base> = {
        _vptr$sp_counted_base = 0x10061b170, 
        use_count_ = 2, 
        weak_count_ = 2
      }, 
      members of boost::detail::sp_counted_impl_pd<Empty*,boost::detail::sp_ms_deleter<Empty> >: 
      ptr = 0x0, 
      del = {
        initialized_ = true, 
        storage_ = {
          data_ = "\000i4\000\001\000\000\000?h4\000\001\000\000", 
          align_ = {<No data fields>}
        }
      }
    }
    

    There’s one thing to note here: the address of the shared count is different: this is a different shared pointer instance… so we’ve somehow created two different shared pointers to the same address. This is an extremely bad thing, since we’d expect this to shortly result in a double-free.

    However, it doesn’t. (I’m quite keen to understand this further, if anyone has any ideas?)

    I don’t know why it doesn’t, to be honest (presumably there is something subtle going on here), but in any case, all this does point to a solution: We can use shared_from_this to create a shared pointer, from the value passed, which we can use to create a weak pointer that actually works.

    So, to wrap up, here’s the fix:

    #include <iostream>
    #include <boost/shared_ptr.hpp>
    #include <boost/python.hpp>
    #include <boost/weak_ptr.hpp>
    #include <boost/enable_shared_from_this.hpp>
    
    namespace bp = boost::python;
    
    struct Empty: boost::enable_shared_from_this<Empty>{ };
    
    struct Store
    {
        boost::weak_ptr<Empty> value;
    
        void setValue(boost::shared_ptr<Empty> const& v) {
            value = boost::weak_ptr<Empty>(v->shared_from_this());
            boost::shared_ptr<Empty> v_ok = value.lock();
            if (v_ok) {
                std::cout << "ok, v has been stored" << std::endl;
            }
        }
    
        boost::shared_ptr<Empty> getValue() {
            boost::shared_ptr<Empty> p = value.lock();
            if (p) {
                std::cout << "stored value : " << p << std::endl;
            } else {
                std::cout << "there's nothing here !" << std::endl;
            }
            return p;
        }
    };
    
    BOOST_PYTHON_MODULE (libmylibinterface)
    {
        bp::class_< Empty, boost::shared_ptr<Empty> >("Empty",bp::init<>())
            ;
    
        bp::class_< Store, boost::shared_ptr<Store> >("Store")
            .def("get",&Store::getValue)
            .def("set",&Store::setValue);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to store an object FOO in a database. Lets say FOO
I have a certificate .p12 and .crt and I would like store the public
I would like to store log4net config data in my application.config file. Based on
I would like to store large dataset generated in Python in a Django model.
I would like to store the cookies from one open-uri call and pass them
I would like to store lengthy .sql scripts in my solution and execute them
I would like to store very large sets of serialized Ruby objects in db
I would like to store a list of entityIDs of outlook emails to a
I would like to store files in a specific directory inside the documents directory,
I would like to store 2010-03-26 10:13:04 Etc/GMT value in column of type datetime.

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.