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 ]
newuser asdf

Posts: 5
Registered: 1/16/11
Re: Making C++ objects persistent between mex calls, and robust.
Posted: Jan 22, 2011 3:55 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hello,

I have some problems in understanding, how this way of object address passing works in my concrete example... Perhaps someone can give me a hint, what I'm doing wrong?

My task: I'm trying to create an instance of a C++ object in a mex file, which then should return its memory address, and another mex function (called afterwards) gets this address as a parameter and calls some functions on it.

I tried to do this in the following way:
I call a mex function, similar to this:
ptr = createObject(.....);

Inside this mex file (createObject) I'm instanciating the object and (trying to) return it:
...
MyObject *obj = new MyObject();
uint64_t ptr = (uint64_t)(obj);
plhs[0]=mxCreateDoublematrix(1,1,mxREAL);
double *resultPtr = mxGetPr(plhs[0]);
*(resultPtr) = ptr;

Then, again in Matlab, I call another mex function, passing this pointer in a structure:
param.ptr = ptr;
doSomething(param);

And in this mex file, I'm trying to get this object, and do something with it:
const mxArray *in = prhs[0];
mxArray *content = mxGetField(in, 0, "ptr");
double *numPtr = mxGetPr(content);
uint64_t ptr = (uint64_t)(*(numPtr));
MyObject *obj = reinterpret_cast<MyObject*>(ptr);
obj->doSomething();

The problem: In this last line, I get an error, and Matlab crashes (probably you might already know the reason?). I've tested the code, deleting the last line (where I "use" the object), and it worked.
I know that with this code I would have a memory leak, but I (would) delete the object later on, through another mex function call... So this is not the problem...

But where is my "big" mistake?
I'm using "new" - so I'm assuming that the object would be persistent; Or do I have to do anything else to assure that (if so, what)?
Or is the fact, that I'm converting the uint64 value to a double (when returning it) a problem?

Can someone give me any suggestion how I can solve this problem, please?

Thanks,
regards,
sabine



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.