Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: Making C++ objects persistent between mex calls, and robust.
Posted:
Jun 8, 2010 5:19 AM


"Anthony Halley" wrote: > Did you ever find a nice solution for this issue? If so, I'd be interested in hearing about it.
Yes. Walter Roberson pointed out the properties of the handle class to me, including it's ability to call a clean up function, here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/283309
So the solution is to define a class which inherits the handle class, something like this:
classdef myclass < handle properties (Hidden = true, SetAccess = private) cpp_handle; end methods % Constructor function this = myclass() this.cpp_handle = init_mex(); end % Destructor function delete(this) clear_mex(this.cpp_handle); end % Example method function output = action(this, data) output = action_mex(this.cpp_handle, data); end end end
I was going to post this when I'd used and tested it, but your question prompted me to post it earlier. As a result, it may not be exactly right, but hopefully you get the gist.
Oliver



