How to supply a custom memory allocator or at the very least existing memory buffer to pybind for object instantiation. #3830
Unanswered
OmerUygurOzer
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
Is there a way to have pybind use a custom memory allocator or at the very least use memory already allocated?
My particular use case is this I have a class called "Event". I bind this like the following:
pybind11::class_<Imperium::Core::Events::Event, Imperium::Core::Events::PyEvent, std::shared_ptr<Imperium::Core::Events::Event>>(m, "Event") .def(pybind11::init<>()) .def("GetEventType", &Imperium::Core::Events::Event::GetEventType, pybind11::return_value_policy::automatic)
Then on python side, I have a few classes that extend from this like:
class TestEvent(mymodule.Event): def __init__(self): imperium.Event.__init__(self); self.mTestString = "TestEvent String" def GetEventType(self): return 123;
These classes are instantiated on the python side as well and they then get passed to an EventManager class like:
GetEventManager().QueueEvent(TestEvent());
My goal is to make sure that the memory allocated to TestEvent is fully management by c++ despite it being created in python. And that whenever it's instantiated, it gets instantiated via some form of in place new operator / allocator supplied by me so that I have full control over where in the memory it is instantiated in.
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions