Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++/MATLAB Mex binding

Tags:

c++

matlab

mex

I'm about to write a Mex File, so I can use my c++ code in MATLAB.

This code is built around a singleton class (in c++).

I've read that for memory to be persistent I have to use MxAlloc isntead of malloc/new, is this true for all memory I use? I.e, the class I have uses vectors, and vectors automatically allocate new memory, using standard mechanisms.

So will a vector in a class allocated using MxAlloc have trouble keeping it's memory?

like image 457
Andreas Mieritz Avatar asked Mar 27 '12 08:03

Andreas Mieritz


1 Answers

You only use MxAlloc for the data that you are going to return to Matlab. Everything that stays within your library can be allocated normally.

One issue you may want to be aware of is that your library can be unloaded at any time. Normally when the user calls your mexFunction the library is loaded and will stay loaded for subsequent calls. However, at anytime Matlab may unload your library and so all the resources in your mexFunction will be freed.

like image 146
Dusty Campbell Avatar answered Oct 13 '22 01:10

Dusty Campbell