How to properly use memory dynamically allocated in C++ dll within Matlab

I wrote a C++ interface dll to call some key image processing statistical measuring methods from Intel’s Integrated Performance Primitives (IPP) library so we can exploit SIMD capabilities. I was able to pass two 2D Matlab arrays over into the function using calllib

A = single( rand(3,4) );
B = single( rand(3,2) );

pOut = calllib('IPPInterface', 'myFunc', A' , B' , size(A,1), size(A,2) , size(B,1), size(B,2) );

Where in C++ the myFunc has the following signature, and snippet of body…

float* myFunc( float A[] , float B[] , int nRowsA , int nColsA , int nRowsB , int nColsB )
    float[] pDst = new float[nRowsA*nColsA];
    return pDst;

What I’m unsure of is how and if Matlab successfully deletes such buffers from memory. They ultimately wind up in the Matlab process as lib.pointers:

>> class(pOut)

ans =


I’m just curious if what I’m doing is a bad idea, or if calling Matlab’s clear pOut would take care of everything and avoid the conceivable memory leak.

Source: dll

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.