Make static variables in mex keep their values between calls

14 views (last 30 days)
I want to integrate external C code consisting of multiple functions and files using simple wrapper function
function out = callExternalCFunction(data_in)
out = coder.ceval('externalCfunction', data_in);
end
and build a MEX file.
I noticed that static variables declared in the external C code do not retain their values between MEX calls. Can I control this behaviour somehow?
  1 Comment
James Tursa
James Tursa on 21 Aug 2020
Edited: James Tursa on 22 Sep 2020
Can you provide simple example code? Static variables should retain their values as long as you don't clear the mex routine from memory and you don't have code that always re-initializes their values when the mex routine is called.

Sign in to comment.

Accepted Answer

Nipun Katyal
Nipun Katyal on 30 Aug 2020
In order to make the variable persistent in memory so as to save its state after every mex call you can use mexMakeMemoryPersistent, and to free the allocated memory you can use the following command,
clear mex
  1 Comment
James Tursa
James Tursa on 22 Sep 2020
Maybe the above works with the MATLAB coder ... I don't know enough about the coder to say what actually gets produced. But this would cause a memory leak in MATLAB in the general case. Using mexMakeMemoryPersistent in a mex routine removes the memory address in question from the garbage collection list. This forces you to have code that explicitly free's this memory in order to recover it. Exiting the mex routine or using "clear mex" will not free this memory .. it would cause a permanent memory leak that could only be fixed by exiting and restarting MATLAB. E.g., using this file:
// Example to demonstrate memory leak
#include "mex.h"
#define MB100 (100*1024*1024)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char *cp;
cp = (char *) mxMalloc(MB100);
mexMakeMemoryPersistent(cp); // This will cause a memory leak
}
Simple runs show the memory leaks and doesn't get recovered by the "clear mex" command:
>> memory
Maximum possible array: 14429 MB (1.513e+10 bytes) *
Memory available for all arrays: 14429 MB (1.513e+10 bytes) *
Memory used by MATLAB: 2263 MB (2.372e+09 bytes)
Physical Memory (RAM): 8100 MB (8.494e+09 bytes)
* Limited by System Memory (physical + swap file) available.
>> memoryleak
>> memory
Maximum possible array: 14332 MB (1.503e+10 bytes) *
Memory available for all arrays: 14332 MB (1.503e+10 bytes) *
Memory used by MATLAB: 2363 MB (2.477e+09 bytes)
Physical Memory (RAM): 8100 MB (8.494e+09 bytes)
* Limited by System Memory (physical + swap file) available.
>> clear mex
>> memory
Maximum possible array: 14327 MB (1.502e+10 bytes) *
Memory available for all arrays: 14327 MB (1.502e+10 bytes) *
Memory used by MATLAB: 2356 MB (2.470e+09 bytes)
Physical Memory (RAM): 8100 MB (8.494e+09 bytes)
* Limited by System Memory (physical + swap file) available.
Memory used by MATLAB goes up 100MB and doesn't drop by 100MB with the "clear mex" command.

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!