Using shared_ptr/cv::Ptr as userdata in callbacks
I am tracking mouse clicks inside an OpenCV display window by using:
void on_buttonClick(int event, int x, int y, int flags, void* userdata)
{
// Callback function received
}
void main() {
cv::setMouseCallback(win_name, on_buttonClick, (void*) fun_ptr);
}
The last parameter fun_ptr
is returned as part of user_data
in the callback. And this is fine as long as I want to use function pointers for userdata.
But now I need to instead pass something equivalent of std::shared_ptr<CustomClass>
as part of user_data
. The reason I want to pass a shared_ptr
is so that the object automatically get destroyed whenever the context of display window is refreshed.
I tried using two mechanisms:
- std::shared_ptr
- cv::Ptr (Similar in functionality to std::shared_ptr)
Example code below:
class CustomClass {
int x;
int y;
}
int main() {
cv::Ptr<CustomClass> x(new CustomClass());
cv::setMouseCallback(win_name, on_buttonClick, (void*) x);
}
But the shared_ptr would lose it's purpose as soon as I cast it to (void*). Is there a way to resolve this in OpenCV (afaik, using the typical new
, delete
would not be good since the time to delete cannot be ascertained).
that's a bit unclear, can you explain ?