1 | initial version |
Detail 2: OpenCV classes in Matlab
There is no ideal solution to referencing C++ classes from Matlab. A set of best practices have been devised by the Matlab community that results in the least amount of undefined behaviour, memory leaks, dangling pointers, etc.
Even so, this requires that the memory address of the C++ object be passed back to Matlab (as a uint64_t) and trusted in the hands of the user (though it is private and invisible).
A better way to do this is to store all of the object instances in a map:
static std::unordered_map<KeyType, OpenCVClass> instances_;
and then give the user a key to accessing their object. That way, if they attempt to modify their key, they'll get a std:out_of_range exception, rather than a segfault. Memory addresses of the objects make good keys since they're not contiguous, so it'll be harder for the user to "guess" another valid instance.