Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with shared_ptr re-entry #14

Closed
lukacu opened this issue Nov 24, 2015 · 2 comments
Closed

Problems with shared_ptr re-entry #14

lukacu opened this issue Nov 24, 2015 · 2 comments

Comments

@lukacu
Copy link
Contributor

lukacu commented Nov 24, 2015

In a project where I am using pybind11 I had a problem with C++ objects wrapped in shared_ptr that were handed over to Python and then re-entered C++. With default approach (using PYBIND11_DECLARE_HOLDER_TYPE) this results in two smart pointers that do not know about each other. Using std::enable_shared_from_this template in my objects I have modified the code generated by PYBIND11_DECLARE_HOLDER_TYPE to something like (basically there is only one small difference at line 7):

template <class T> class type_caster<std::shared_ptr<T>> : public type_caster_holder<T, std::shared_ptr<T>> { 
    typedef type_caster<T> parent;
public:
    bool load(PyObject *src, bool convert) {
        if (!parent::load(src, convert))
            return false;
        holder = ((T *) parent::value)->shared_from_this();
        return true;
    }
    explicit operator T*() { return this->value; }
    explicit operator T&() { return *(this->value); }
    explicit operator std::shared_ptr<T>&() { return holder; }
    explicit operator std::shared_ptr<T>*() { return &holder; }
    using type_caster<T>::cast;
    static PyObject *cast(const std::shared_ptr<T> &src, return_value_policy policy, PyObject *parent) {
        return type_caster<T>::cast(src.get(), policy, parent);
    }
protected:
    std::shared_ptr<T> holder;
};

I hope that this will save some time to anyone who will have the same scenario. If there is a good way to support this by default in some way it would be also very nice. I am not that familiar with C++ templates, but I guess the template could be limited to classes that inherit from std::enable_shared_from_this.

@wjakob wjakob closed this as completed in 8a34f70 Nov 24, 2015
@wjakob
Copy link
Member

wjakob commented Nov 24, 2015

Hi Luka,

thanks for tracking that down -- the fix ended up being a bit more involved. Please double-check that it works for you and let me know in case it doesn't.

Thanks,
Wenzel

wjakob pushed a commit that referenced this issue Nov 24, 2015
@lukacu
Copy link
Contributor Author

lukacu commented Nov 24, 2015

Wow, that was fast. I will check it out as soon as I can, but just looking at the code it appears that the fix is integrated well. Thanks.

knarfS added a commit to knarfS/pybind11 that referenced this issue Apr 10, 2020
knarfS added a commit to knarfS/pybind11 that referenced this issue Apr 11, 2020
knarfS added a commit to knarfS/pybind11 that referenced this issue Apr 20, 2020
knarfS added a commit to knarfS/pybind11 that referenced this issue Aug 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants