Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Making C++ objects persistent between mex calls, and robust.
Replies: 37   Last Post: Sep 27, 2013 6:54 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Francesco Montorsi

Posts: 3
Registered: 11/7/12
Re: Making C++ objects persistent between mex calls, and robust.
Posted: Nov 7, 2012 12:19 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Dear Oliver,
first of all, thank you very much for writing the initial coincise and clear post. I was searching in google for this problem and reading this thread helped me very much.
Indeed, I managed to interface my C++ class with MATLAB defining a class derived from "handle" in MATLAB and then using new/delete and the convertMat2HandlePtr / convertPtr2Mat methods in C++ .

Just a suggestion: why don't you create a FileExchange contribution with the C++ code and the MATLAB class wrapper? I think it would be very useful to anybody tackling this same issue! :)

Finally, a question about mexLock() and mexUnlock() usage suggested in the last post: it doesn't work for me: mexLock() gets called in convertPtr2Mat() in the init_mex() MEX file, so that it will lock the "init_mex resource", but then mexUnlock() gets called in destroyObject() which is located in the clear_mex() MEX file, and will try to unlock the "clear_mex resource", which is already unlocked. Most important since the mexUnlock() call is located in a different MEX file, the one which called mexLock() will remain locked forever...
did I miss something?
do you implement the clear_mex() and init_mex() functions in the same MEX file? if so, how do you do that (as far as I know, there is only one mexFunction() possible in each MEX file)...

Thank you very much!
Francesco





>
> template<class base> inline class_handle<base> *convertMat2HandlePtr(const mxArray *in)
> {
> if (mxGetNumberOfElements(in) != 1 || mxGetClassID(in) != mxUINT64_CLASS || mxIsComplex(in))
> mexErrMsgTxt("Input must be a real uint64 scalar.");
> class_handle<base> *ptr = reinterpret_cast<class_handle<base> *>(*((uint64_t *)mxGetData(in)));
> if (!ptr->isValid())
> mexErrMsgTxt("Handle not valid.");
> return ptr;
> }
>
> template<class base> inline base *convertMat2Ptr(const mxArray *in)
> {
> return convertMat2HandlePtr<base>(in)->ptr();
> }
>
> template<class base> inline void destroyObject(const mxArray *in)
> {
> delete convertMat2HandlePtr<base>(in);
> mexUnlock();
> }
>
> // Code for init_mex() - ISM3D is my C++ object class, but you use your own
> ISM3D *ism = new ISM3D(var1, var2);
> plhs[0] = convertPtr2Mat<ISM3D>(ism);
>
> // Code for clear_mex()
> destroyObject<ISM3D>(prhs[0]);
>
> // Code for action_mex
> ISM3D *ism = convertMat2Ptr<ISM3D>(prhs[0]);
> ism->action();



Date Subject Author
4/1/10
Read Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
6/7/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Anthony Halley
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Walter Roberson
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Anthony Halley
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
1/22/11
Read Re: Making C++ objects persistent between mex calls, and robust.
newuser asdf
1/22/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Bruno Luong
1/23/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Rune Allnor
1/23/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Bruno Luong
1/23/11
Read Re: Making C++ objects persistent between mex calls, and robust.
newuser asdf
1/24/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
2/3/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Pierre
2/3/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
2/3/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Pierre
2/3/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
2/15/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Pierre
7/6/11
Read Re: Making C++ objects persistent between mex calls, and robust.
mathias
7/6/11
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
5/24/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Ajay
5/25/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Pierre
10/2/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
11/7/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Francesco Montorsi
11/8/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
11/9/12
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
1/21/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
1/22/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
1/30/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Gabri Gwala
1/30/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Steven Lord
1/31/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
9/22/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Gabri Gwala
9/22/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Gabri Gwala
9/27/13
Read Re: Making C++ objects persistent between mex calls, and robust.
Oliver Woodford
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Anthony Halley
6/8/10
Read Re: Making C++ objects persistent between mex calls, and robust.
Anthony Halley

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.