C function returning an mxArray pointer

조회 수: 12 (최근 30일)
Joel Andersson
Joel Andersson 2011년 9월 1일
댓글: Marleen 2025년 7월 23일
Hello!
I am trying to interface our software to Matlab using the functionality to call external, shared libraries from Matlab using loadlibrary and calllib.
My question is:
If I have a C function returning a mxArray pointer: mxArray* myCFunction(mxArray* args);
Will then Matlab be responsible for deleting the object? What If the function returns a null pointer or the input arguments, is this forbidden, e.g.: mxArray* myCFunction(mxArray* args){ return args; }
or
mxArray* myCFunction(mxArray* args){ return 0; }
Best, Joel

채택된 답변

Philip Borghesani
Philip Borghesani 2011년 9월 1일
The rules are the same as for outputs returned via plhs in a mex file.
In short:
  • To return an input a copy must be made with mxDuplicateArray.
  • MATLAB will delete the object when there are no longer any references to it in MATLAB.
  • A null pointer should convert to an empty double array but test this if you depend on it.
  댓글 수: 2
Joel Andersson
Joel Andersson 2011년 9월 2일
Alright. Thank you for clarifying!
Marleen
Marleen 2025년 7월 23일
Hello, this is outdated information as of matlab 2025a. My DLL functions return mxArray*. On error state it used to return NULL. On matlab side, a calllib call would just throw error as if no output argument is there. So it doesn't even convert to an empty double array. My fix was to change the error handling to use mexErrMsgIdAndTxt, which works wonderfully.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by