I implemented a C-API for a C++ class which uses shared-pointers of other objects to access them. In my C-API I can of course only get raw pointers. So I "convert" the raw pointer in my C-API to a shared-pointer and use this then with my C++ class methods:
method(std::shared_ptr<dataType>(raw-pointer));
Now I have the problem that at the end of "method" always the shared-pointer destructor is called and it unfortunately kills the object my raw-pointer is pointing at (which I don't want). So, how can I prevent the raw-pointer from being killed?
I already tried shared-pointer functions like reset() or swap(), but they all didn't let my raw-pointer go...
bool Traffic_doStep(traffic_handle t, environment_handle e, double cycletime) {
if (!valid(t, __FUNCTION__)) return false;
if (!valid(e, __FUNCTION__)) return false;
if (!valid(cycletime, __FUNCTION__)) return false;
try {
t->doStep(std::shared_ptr<Environment>(e), cycletime);
return true;
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return false;
}
}
Expected result would be that the raw-pointer e is still pointing to a valid object after this function returned. Actually the raw-pointer points then to a deleted object.