MATLAB: Do I have memory leak when I use the CALLLIB function in MATLAB 7.0.1 (R14SP1)

calllibleaklibpointerMATLABmemory

When I use the commands:
loadlibrary('testdll', 'testdll.h');
theclass = calllib('testdll', 'xxx_Construct');
len = 4096;
b = zeros(4096, 1, 'uint8');
pb = libpointer('uint8Ptr', b);
for q=1:N
[x,ret] = calllib('testdll', 'xxx_DoIt', theclass, len, pb);
end
Where "theclass" is a pointer to a C++ class that defines the "xxx_DoIt" function, and "pb" is a libpointer to a MATLAB matrix, I see that after each call to the CALLLIB function, MATLAB consumes more and more memory, even though "xxx_DoIt" function does not allocate any memory.

Best Answer

This bug has been fixed in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds:
There is a bug in MATLAB 7.0.1 (R14SP1) when calling CALLLIB function with 2 pointers as input arguments, and one of those pointers is a libpointer.
To work around this issue, the testdll.h file should be changed.
Instead of using a statement like:
typedef void* CLASS_HANDLE;
where "theclass" pointer is a CLASS_HANDLE type, use the code:
#ifdef TESTDLL_EXPORTS
typedef void* CLASS_HANDLE;
#else
typedef unsigned long CLASS_HANDLE;
#endif