Make static variables in mex keep their values between calls

20 visualizzazioni (ultimi 30 giorni)
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 Commento
James Tursa
James Tursa il 21 Ago 2020
Modificato: James Tursa il 22 Set 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.

Accedi per commentare.

Risposta accettata

Nipun Katyal
Nipun Katyal il 30 Ago 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 Commento
James Tursa
James Tursa il 22 Set 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by