Thu 09 Jun 2016 01:57:32 AM UTC, original submission:
It doesn't look like mexCallMATLAB exits the routine when an error occurs on the first call. Subsequent calls exit normally. In general, mexCallMATLAB should immediately exit the mex file on error, which makes the return value a little superfluous. Consider the code:
$ cat test01.cpp
#include "mex.h"
#include <array>
#include <iostream>
void mexFunction(
int nOutput,mxArray* pOutput[],
int nInput,mxArray const * pInput[]
) try {
// Calling convention should be
// f_x = test01(f,x)
auto f = const_cast <mxArray*> (pInput[0]);
auto x = const_cast <mxArray*> (pInput[1]);
// Call the function
auto input = std::array <mxArray *,2> {f,x};
auto output = std::array <mxArray *,1> ();
auto err=mexCallMATLAB(1,output.data(),2,input.data(),"feval");
if(err)
throw std::runtime_error("There's a MATLAB/Octave error");
// Assign the return value
pOutput[0]=output[0];
// Return nothing
return;
} catch(std::runtime_error const & e) {
std::cerr << "Exception!!!!!" << std::endl;
return;
}
which is compiled with:
g++ -std=c++14 -g -fPIC -I /usr/include/octave-4.0.0/octave/ test01.cpp -shared -o test01.mex
And the driver code:
$ cat driver01.m
f = @(x)x+1
x = {1}
y = test01(f,x)
This should error since we can't add a cell array to a number. When we run the routine:
$ octave
octave: no graphical display found
octave: disabling GUI features
GNU Octave, version 4.0.0
Copyright (C) 2015 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'.
Octave was configured for "x86_64-pc-linux-gnu".
Additional information about Octave is available at http://www.octave.org.
Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html
Read http://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.
octave:1> driver01
f =
@(x) x + 1
x =
{
[1,1] = 1
}
error: binary operator '+' not implemented for 'cell' by 'scalar' operations
error: called from
driver01> at line -1 column -1
driver01 at line 3 column 3
Exception!!!!!
y = [](0x0)
We see that the exception is thrown and that y is assigned a null value. If we run the routine a second time:
octave:2> driver01
f =
@(x) x + 1
x =
{
[1,1] = 1
}
error: binary operator '+' not implemented for 'cell' by 'scalar' operations
error: called from
driver01> at line -1 column -1
driver01 at line 3 column 3
The exception is not thrown and y is not assigned a value. I believe the behavior in the second case to be correct and the behavior in the first case to be a bug.
|