MATLAB: What’s wrong with this trivial C engine program

cenginemex

I'm trying to learn how to run Matlab from C programs, and apparently writing mex files to interface with the Matlab engine is the way to do that. This is Matlab 2009a running on Win32 with MSVC 9 as the compiler.
I made the very simple program below that just allocates an array of structures and then deallocates it, repeating this 100 times. There are ARRAY_SIZE structures in the array. Each structure has three members and each member is a 10×10 matrix. Everything works fine if ARRAY_SIZE is about 5, but slightly larger numbers such as 20 cause the program to crash every time. Anyone know what's wrong with this code? Could I possibly be running out of heap or stack space? Does it have something to do with freeing the array after it has been "put" into the engine's workspace? Thanks for any help~
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <direct.h>
#include "engine.h"
#define ARRAY_SIZE 5
int main(void)
{
Engine *ep;
ep = engOpen(NULL);
engSetVisible(ep, 1);
const char *field_names[] = {"a", "b", "c"};
mxArray *array;
mxArray *field_value;
int foo = 0;
int j, k;
while(foo < 100)
{
array = mxCreateStructMatrix(1, ARRAY_SIZE, 3, field_names);
for(j = 0; j < ARRAY_SIZE; ++j)
{
for(k = 0; k < 3; ++k)
{
field_value = mxCreateDoubleMatrix(10, 10, mxREAL);
mxSetFieldByNumber(array, j, k, field_value);
}
}
engPutVariable(ep, "array", array);
for(j = 0; j < ARRAY_SIZE; ++j)
{
for(k = 0; k < 3; ++k)
{
field_value = mxGetFieldByNumber(array, j, k);
mxDestroyArray(field_value);
}
}
mxDestroyArray(array);
++foo;
}
engClose(ep);
return 0;
}

Best Answer

Do not destroy the fields of the struct manually. This works:
...
engPutVariable(ep, "array", array);
/* deleted: for (j=0 ... mxDestroyArray(field_value) ... */
mxDestroyArray(array);
...
Destroying the fields does not clear the corresponding pointer in the struct. But destroying the struct clears the fields automatically. Your program seems to worked for small array sizes just by accident. But the memory management is corrupted immediately.