MATLAB: Matlab problem with ext. library and large c++ structures

cMATLAB

I am writing a dll (with C++) for use with matlab. The dll has several function calls which look like this:
int8 function1(paraStruct* const Params);
The first weeks the dll worked fine, but as development continued the Params structure grew larger, and at some point matlab started to behave erratic:
-it crashed about 20 seconds after loading the dll with loadlibrary without any warning (the logfile was empty). -I got segmentation vaults when calling functions -the dll did not return correct data
None of these problem occurred when I called the dll from another C++ program, so I'm quite sure matlab is at least part of the problem. I'm using matlab R2007b, but a coworker had similar problems with R2010b.
At the moment, the structure looks like this:
typedef struct
{
uint16 u16_Depth;
uint16 u16_Width;
uint16 u16_Height;
uint16* pu16_InData;
uint16* pu16_OutData;
uint16 u16_Start;
uint16 u16_End;
uint16 u16_Flags;
uint8 u8_NR_Algorithm;
uint8 u8_NR_Border;
uint8 u8_NR_Width;
uint8 u8_NR_Height;
float32 f32_NR_Sigma;
uint8 u8_HR_Algorithm;
uint8 u8_HR_Flags;
float32 f32_HR_Area;
float32 f32_CA_Intensity;
uint16 u16_CA_Height;
uint16 u16_CA_Border;
uint8 u8_CA_Flags;
uint8 u8_ED_Line;
uint8 u8_ED_Algorithm;
uint8 u8_ED_Flags;
uint8 u8_ED_Mode;
int16* pi16_ED_CustomMode;
uint8 u8_ED_Border;
uint16 u16_ED_Width;
uint16 u16_ED_Height;
float32* pf32_ED_Sigma;
uint8 u8_ED_Meta;
uint8 u8_ED_Upper;
uint8 u8_ED_Lower;
float32* pf32_ED_UpperList;
float32* pf32_ED_LowerList;
float32 f32_ED_Start;
float32 f32_ED_End;
uint16 u16_ED_Number;
}
paraStruct;
When I remove some values from the struct (i.e. all ED values) it works again, but of course only with limited functionality.
Is there some way to persuade matlab to work with such abundand structures, or at least some feasible workarount?
Yours, Lasse

Best Answer

Hi Philip,
I made a little progress since two days ago. I thought about packing, too, but since the dll worked nice for some time with gaps in it I didn't follow this direction at first.
Anyway, using #pragma pack I could change the packing of the structure in the C-Code, but MatLab always returned the same structsize regardless of the packing factor (I did use only the header file, not a custom loader file).
For now, I've filled the gaps in the structure with dummy values and changed one pointer to an array. The stability is much better now, but I have a new problem with the array.
The structure now looks like this in C-code (truncated, the remaining code is as above):
(...)
//Byte offset in structure (decimal)
uint16 u16_ED_Height; // 64
uint16 fill_6; // 66 - a dummy/fill value
float32 f32_Sigma[8]; // 68
uint8 u8_Meta; //100
uint8 u8_ED_Upper; //101
uint8 u8_ED_Lower; //102
uint8 fill_7; //103
(...)
Until fill6 MatLab interprets the structure fine, but it reads the first value of array Sigma at offset 72, not 68, the second at 76 instead of 72, and so on. The 4 byte difference continues for the rest of the structure. The data at offset 68 on the other hand is omitted.
Any idea?