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

3 views (last 30 days)
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

Accepted Answer

Lasse B.
Lasse B. on 20 Oct 2011
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?
  2 Comments
Philip Borghesani
Philip Borghesani on 20 Oct 2011
I believe you are hitting this: http://www.mathworks.com/support/bugreports/400557 fixed in R2008a. MATLAB should honor the packing value from the header file can you create a loader file and examine the packing specified in it to see if the header file is being processed correctly?
Lasse B.
Lasse B. on 21 Oct 2011
Hi Philip,
thanks for input. The loader file indeed solves the problem with the array offsets. Looks good now!

Sign in to comment.

More Answers (1)

Philip Borghesani
Philip Borghesani on 19 Oct 2011
Provided you are properly handling the pointers in this structure the problem is probably caused by structure packing. If there are no #pragma pack(n) statements in your header than loadlibrary has to guess at the default packing used to build the dll. The solution is to add a #pragma pack statement to your header or to change the packing specified in a custom loader file. See loadlibrary help on mfilename for information on custom loader files.
On way to verify that MATLAB properly interpreted the structure is to create a libstruct object of your structure type and call its structsize method. The size should be the same as that from sizeof in C.
cd(fullfile(matlabroot,'extern','examples','shrlib'))
loadlibrary shrlibsample
s=libstruct('c_struct')
s.structsize
ans =
16|MONOSPACED TEXT|

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!