swig
swig copied to clipboard
std::string with more than 255 characters fails type check
An std::string passed as an argument to a function from matlab fails the type check when the string length is greater than 255 characters. The SWIG_Matlab_ConvertPtrAndOwn
method returns error in the first condition in it's body when checking for mxGetNumberOfElements(pm_ptr) != 1
. The exact same code works correctly for shorter strings.
To reproduce this, compile the following input file:
%module test
%{
#include <string>
#include <iostream>
void myStringFunction(std::string str)
{
std::cout << "Your string was:\n" << str << "\n";
}
%}
%include std_string.i
%inline %{
void myStringFunction(std::string str);
%}
In matlab:
s=repmat('test string ',1,100);
test.myStringFunction(s(1:255)); % Works fine
test.myStringFunction(s); % Fails inside type check
I have compiled the code with c++11 flags enabled.
The problem is related to the internal buffer used for the conversion. See this line: https://github.com/jaeandersson/swig/blob/matlab/Lib/matlab/matlabprimtypes.swg#L235
It might be needed to increase the size of this static buffer or use a dynamic array.