Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #54 from robotpy/fix-crash
Browse files Browse the repository at this point in the history
Wrap VideoListener to hold a reference to the callback
  • Loading branch information
virtuald committed Jan 2, 2019
2 parents 7c0873d + e1c016b commit e692c7b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/_cscore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ using namespace cs;
typedef py::call_guard<py::gil_scoped_release> release_gil;



// Wrapper for VideoListener to prevent crashes on shutdown
class VideoListenerWrapper : public VideoListener {
public:
VideoListenerWrapper(std::function<void(const VideoEvent&)> callback, int eventMask, bool immediateNotify) :
VideoListener(callback, eventMask, immediateNotify),
m_callback(callback)
{}

virtual ~VideoListenerWrapper() {}

private:
// need a reference to this callback otherwise cscore crashes on exit
std::function<void(const VideoEvent&)> m_callback;
};


PYBIND11_MODULE(_cscore, m) {

NDArrayConverter::init_numpy();
Expand Down Expand Up @@ -582,7 +599,7 @@ PYBIND11_MODULE(_cscore, m) {
.def("getProperty", &VideoEvent::GetProperty);


py::class_<VideoListener> videolistener(m, "VideoListener");
py::class_<VideoListenerWrapper> videolistener(m, "VideoListener");
videolistener.doc() =
"An event listener. This calls back to a desigated callback function when\n"
"an event matching the specified mask is generated by the library.";
Expand Down

0 comments on commit e692c7b

Please sign in to comment.