I am trying to print the memory address of the same object in both c++ and python with pybind11, but I found the returned memory address from both are not identical.
c++ side
class Example {
public:
Example() {std::cout << "constuctor" << this << std::endl;}
~Example() {std::cout << "destructor " << this << std::endl;}
};
class ABC {
public:
static std::unique_ptr<Example> get_example() {
// std::shared_ptr<Example> ptr = std::make_shared<Example>();
std::unique_ptr<Example> ptr = std::make_unique<Example>();
return ptr;
}
};
void init_example(py::module & m) {
py::class_<ABC>(m, "ABC")
.def_static("get_example", &ABC::get_example);
}
python side
example = my_module.ABC.get_example()
print (example)
the output
constuctor0x234cd80
<Example object at 0x7f5493c37928>
destructor 0x234cd80
the memory address from c++ is 0x234cd80, but python is 0x7f5493c37928
any idea?
thisin the C++ code, but in actually inget_example, you're creating astd::unique_ptr, and its address is not going to be the same asthis. See this example. Issue seems to have nothing to do with Python.std::unique_ptris the same as the address of the object it points to.get()function, by whatever means Python gives you.