memory leak from mex file in R2012a?

2 views (last 30 days)
Matthew Gillman
Matthew Gillman on 24 Jun 2015
Answered: Jan on 30 Jun 2015
Hi
At university I am using MATLAB version 7.14.0.739 (R2012a) on a Linux Ubuntu 32-bit system.
I am using code which calls a C++ mex file repeatedly. However, after a certain number of iterations I run out of memory.
As far as I can tell the C++ code, which was written by someone else, is being used correctly, and also I clear MATLAB variables as often as possible. But this still happens.
I don't know if I'll be able to upgrade my university version of MATLAB, so is there a memory leak known issue with mex files in R2012a?
Thanks
Matthew

Answers (2)

Philip Borghesani
Philip Borghesani on 24 Jun 2015
Most likely the problem is in the specific mex file. There are no systematic leaks with mex files in any recent version of MATLAB.
  5 Comments
James Tursa
James Tursa on 29 Jun 2015
This is too much code to sift through. Can you simplify it to a much smaller size that still reproduces the problem?
Matthew Gillman
Matthew Gillman on 30 Jun 2015
Hi there.
Yes, sorry, I realise it was too much :-)
I refactored my code so I had a perl wrapper which continuously called my MATLAB code (for one image), then exited MATLAB so as to free up memory, then called it again, etc. But I still got a memory allocation error after a while.
I realised that the problem was not a memory leak (where memory is gradually lost over time). Rather, the problem was with the size of the matrices that MATLAB was trying to allocate. The size of these matrices depends on files which specify the number of "feature points" (points of interest) in an image. It couldn't handle a file with 7678 points, but managed 6552, so I am guessing the limit is somewhere between these two, and basically to get round it I will have to only process those files which do not have too many feature points in. This should stop the memory allocation errors.
Thanks
Matthew

Sign in to comment.


Jan
Jan on 30 Jun 2015
Are you sure that the 3rd input of the Mex function is a double?
double const *flag = (double *)mxGetData(prhs[2]);
In the caller it is mentioned as a flag with the values 0 and 1. It could be a logical also. So prefer:
double flag = mxGetScalar(prhs[2]);
It is better to rely on mwSize instead of int:
int const *dims1 = mxGetDimensions(prhs[0]);
mxGetDimension does not reply int.
But I do not think that this solves your problem.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!