Passing struct which has a double pointers when calling C library

8 views (last 30 days)
Hello All,
I want to pass a struct to a native C dynamic library. The struct has a double pointer in it as a member.
struct imageMetaData
{
uint16_t height;
uint16_t width;
};
struct imageFrame
{
struct imageMetaData metaData;
uint16_t **imageData;
};
So, as you can see above, I have the **imageData as a member of a struct. I need to pass the struct imageFrame as a function parameter like -
int writeFrame(int num, struct imageFrame *frame)
I need to allocate the imageData memory which consists of multiple images. Is there a way in MATLAB to do this?
I have tried the following code in MATLAB -
H = 100;
W = 100;
num = 5;
imageMetaData = {};
imageMetaData.height = H;
imageMetaData.width = W;
imageFrame = {};
imageFrame.imageMetaData = libstruct('imageMetaData', imageMetaData);
imageData = ones(num, H * W);
imageFrame.imageData = libpointer('uint16PtrPtr', imageData);
frameC = libstruct('imageFrame', imageFrame);
err = calllib('myDll', 'writeFrame', num, frameC);
The last call fails and I get an exception with MATLAB crashing.
For reference, I am using MATLAB2015a and Visual Studio 2013 (both 64 bit).
I have also added an attachment which has a sample code in C as well as the MATLAB code which causes the crash. I have also attached the crash dump for reference in the same attachment. Note that to compile the C code, you will have to use CMake or else just create a new project with the .cpp and .h files.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!