What's wrong with this trivial C engine program?

1 view (last 30 days)
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 10x10 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;
}

Accepted Answer

Jan
Jan on 17 Aug 2011
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.
  1 Comment
James Tursa
James Tursa on 17 Aug 2011
@Andy: What Jan has shown is the best way. But FYI, what you did would have worked had you added one line in your destroy loops:
field_value = mxGetFieldByNumber(array, j, k);
mxDestroyArray(field_value);
mxSetFieldByNumber(array, j, k, NULL); /* Added this line */

Sign in to comment.

More Answers (1)

Andy Yancy
Andy Yancy on 17 Aug 2011
Works great, thanks very much Jan and James!

Community Treasure Hunt

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

Start Hunting!