MATLAB: Returning mxArray* as void* from a C shared library

callibMATLABmexmxarraymxgpuarrayshared library

For a C function that inputs an mxArray*, one can simply:
calllib(mylib, 'myinputfun', myarray);
If the C function instead inputs a void* (reinterpreted within the function as an mxArray*), then this works:
calllib(mylib, 'myinputfun', libpointer('MATLAB array', myarray));
Similarly, if the C function returns an mxArray*:
myarray = calllib(mylib, 'myoutputfun');
When returning a void* (that actually is an mxArray*), a libpointer with a voidPtr type is returned, which makes sense. However I'm having difficulty accessing the underlying value. I would expect something like this to work, but I cannot seem to cast the pointer to a 'MATLAB array'.
mypointer = calllib(mylib, 'myoutputfun');
setdatatype(mypointer, 'MATLAB array', size);
myarray = mypointer.Value;
I am aware that it would be simpler to work with native types like double*. The reason I require this is because the functions I have are actually creating and returning mxGPUArray*, and I need to preserve the array structure in order preserve its data pointer to GPU memory (hope that makes sense).

Best Answer

Passing mxArrays via void pointers is likely to cause problems in your code (memory leaks and crashes). The calllib code has special handling for transferring ownership of mxArrays when needed but it can't do this when they are passed as a void pointer. If you whish to avoid including mex.h I suggest adding a forward deceleration for mxArray to your header file instead:
typedef struct mxArray mxArray;