How to exchange data between two C++ MEX files

4 views (last 30 days)
Good morning,
I need to write a two (or more) C++ mex functions
  1. the first MEX function allocates dynamically a std::vector<double> and fills it with some data,
  2. the second MEX function makes some other computations on the same vector allocated by the first MEX function.
Indeed I managed to do it in the following way:
  • the first MEX function invokes mexLock so as to prevent the memory to be released after execution, allocates the vector, and returns to Matlab its address into a uint64 integer ptr_vec:
std::vector<double> *p_data = new std::vector<double>;
std::vector<double> &data = *p_data;
// Fill the vector with some data
ptr_vec = reinterpret_cast<uint64_t>(p_data); // ptr_vec is returned to MATLAB
  • The second MEX function accepts this pointer ptr_vec, and does some job:
std::vector<double> &data = *reinterpret_cast<std::vector<double> *>(ptr_vec);
Left aside the burden of avoiding memory leaks and the danger of using "naked" (= not smart) pointers, on my PC the programs work fine (Matlab R2021b on Linux Fedora 34). However, my questions are:
  1. Is this the right way to preceed? Is this kind of technique reliable and portable over other architectures / MATLAB versions?
  2. It is mandatory to invoke mexLock in the first MEX function?
Thanks
R.S.

Accepted Answer

James Tursa
James Tursa on 17 Jan 2022
Your current scheme is not foolproof. How does the 1st mex function know when it is safe to unlock and clear memory? How does the 2nd mex function know that the pointer it was passed is indeed safe to use? Etc. etc. It winds up putting all of this burden on the user, and users can make mistakes which in this case would lead to corruption and crashes.
I would adivse instead to encapsulate everything into one mex routine, and pass in an extra parameter that dictates the actual function you want performed. E.g., maybe you pass in a string and 'allocate' would do the 1st function, and 'operate' would do the 2nd function, and 'cleanup' would free the memory and reset pointers. Etc. The 'operate' function has direct access to everything so it will know if the memory is allocated and the pointer is valid. If you called the mex routine in 'operate' mode and it detects that the pointer is invalid it can throw an error with a sensible message instead of crashing. If you called the mex routine in 'allocate' mode it can detect if the pointer is already allocated and take appropriate action (throw an error, or maybe deallocate and reallocate). In short, all of the memory checking and allocation/deallocation that you want to have happen is much easier if it is all in one dll instead of scattered throughout several different dll's.
  1 Comment
Riccardo Scorretti
Riccardo Scorretti on 17 Jan 2022
Yes, I agree that the scheme I proposed is tricky and error prone; I would have liked to avoid to have a single source code. Thank you very much.
R.S.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB Coder in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!