From: Jesse Hopkins on
I am working on a c-mex function, and I want to make sure I am using mexMakeMemoryPersistent properly. There is a buffer which periodically grows using mxRealloc. Currently, I call mexMakeMemoryPersistent after each call to mxRealloc, is that the proper usage, or does it only need to be called initially when the buffer is allocated?

psuedocode:
pBuffer = mxRealloc( pBuffer,NEW_BUFFER_SIZE);
mexMakeMemoryPersistent(pBuffer);
From: James Tursa on
"Jesse Hopkins" <na(a)na.com> wrote in message <i1o5k6$pbg$1(a)fred.mathworks.com>...
> I am working on a c-mex function, and I want to make sure I am using mexMakeMemoryPersistent properly. There is a buffer which periodically grows using mxRealloc. Currently, I call mexMakeMemoryPersistent after each call to mxRealloc, is that the proper usage, or does it only need to be called initially when the buffer is allocated?
>
> psuedocode:
> pBuffer = mxRealloc( pBuffer,NEW_BUFFER_SIZE);
> mexMakeMemoryPersistent(pBuffer);

I think you are doing this correctly. The MATLAB memory manager doesn't know your intentions with the new block unless you specifically tell it. You can probably easily find out by *not* doing the mexMakeMemoryPersistent and then watching to see if MATLAB crashes. My guess is it will. I'm not sure what happens if you get the same block back ... i.e. what if the new size is the same as the old size and you get the same block back. Is it still persistent? I don't know. Probably a good idea to make it persistent with another call just to be sure.

FYI, there is a bug in mxRealloc for later versions of MATLAB if you ever use it to shrink your memory blocks ... mxRealloc does not generate a new block and does not free up any memory.

James Tursa