Skip to content

[smart_holder] RuntimeError: Invalid return_value_policy for shared_ptr #3011

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

Closed
jakobandersen opened this issue May 18, 2021 · 5 comments
Closed

Comments

@jakobandersen
Copy link
Contributor

While trying to convert from Boost.Python I ran into the lifetime issue raised, e.g., in #2839, so I am using the smart_holder branch.
A minimal test case can be found at https://github.com/jakobandersen/pyTest/tree/pybind, with the central part at https://github.com/jakobandersen/pyTest/blob/pybind/libpytest.cpp#L42.

Essentially I have a custom function class which can be derived in Python. The problem is occurs when the function takes a std::shared_ptr as argument and is called from C++. When using PYBIND11_OVERRIDE_PURE_NAME to implement the call back to the Python implementation it will call the override with the default return_value_policy, which seems to be automatic_reference.
This gives the exception RuntimeError: Invalid return_value_policy for shared_ptr..
Expanding the macro and giving automatic as policy makes it work as intended (as done in https://github.com/jakobandersen/pyTest/blob/pybind/libpytest.cpp#L58).

Am I missing something in the setup? Is this manual hax to make it work correct?

@jakobandersen
Copy link
Contributor Author

A simpler minimal case than above:

class C {
	C(int data);
public:
	int data;
public:
	static std::shared_ptr<C> make(int data) {
		return std::shared_ptr<C>(new C(data));
	}
};

void trigger() {
	auto l = py::list();
	auto c = C::make(42);
	l.append(c); // throws RuntimeError: Invalid return_value_policy for shared_ptr.
}

I'm not sure if this intersects the same code as the original example.

@jakobandersen jakobandersen changed the title [smart_holder] Passing shared_ptr to callback and return_value_policy [smart_holder] RuntimeError: Invalid return_value_policy for shared_ptr May 21, 2021
@jakobandersen
Copy link
Contributor Author

As noted in #3012, the original issue seems fixed in the smart_holder branch, but the second issue is still present.

@rwgk
Copy link
Collaborator

rwgk commented Jun 15, 2021

Hi @jakobandersen, echoing @rhaschke 's request: #3012 (comment)

That would help us working on the issue together.

BTW: I will respond to PRs much faster, I just don't have the free bandwidth to pay much attention to the pybind11 github issues.

@jakobandersen
Copy link
Contributor Author

Hi @jakobandersen, echoing @rhaschke 's request: #3012 (comment)

That would help us working on the issue together.

Sure, I started working in #3039, at least for adding tests for the cases I ran into and see if a simple change works out.

@jakobandersen
Copy link
Contributor Author

Fixed via #3039.

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