jaeandersson / swig

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.
http://www.swig.org
Other
23 stars 19 forks source link

Bug in handling of non-member variables #68

Closed jaeandersson closed 8 years ago

jaeandersson commented 8 years ago
checking matlab testcase arrays_global (with run test)
arrays_global_wrap.cxx: In function 'int _wrap_BeginString_FIX44d_set(int, mxArray**, int, mxArray**)':
arrays_global_wrap.cxx:2478:36: warning: deleting array 'BeginString_FIX44d'
   if (BeginString_FIX44d) delete[] BeginString_FIX44d;
                                    ^~~~~~~~~~~~~~~~~~
arrays_global_wrap.cxx:2481:112: error: ISO C++ forbids casting to an array type 'char []' [-fpermissive]
     BeginString_FIX44d = (char [])reinterpret_cast< char* >(memcpy((new char[size]), arg1, sizeof(char)*(size)));
                                                                                                                ^
arrays_global_wrap.cxx:2481:112: error: incompatible types in assignment of 'char*' to 'char [8]'
arrays_global_wrap.cxx:2483:26: error: incompatible types in assignment of 'int' to 'char [8]'
     BeginString_FIX44d = 0;
                          ^
make[2]: *** [matlab_cpp] Error 1
make[1]: *** [arrays_global.cpptest] Error 2
jaeandersson commented 8 years ago

The problem appears to be that the matlab bindings emits a typemap for char [] when they shouldn't. If you look at the same code for Python, you get:

SWIGINTERN int Swig_var_BeginString_FIX44d_set(PyObject *_val) {
  SWIG_exception_fail(SWIG_ArgError(SWIG_AttributeError), "in variable '""read-only BeginString_FIX44d""' of type '""char []""'");
  return 0;
fail:
  return 1;
}

i.e. no matching typemap. But for some reason, the MATLAB bindings do find a matching typemap, but one which doesn't compile.

@wsfulton - any ideas?

wsfulton commented 8 years ago

You can analyse which typemaps are being applied by using the -debug-tmused and/or debug-tmsearch options. Compare the two target languages. I get:

./../arrays_global.i:45: Searching for a suitable 'varin' typemap for: char BeginString_FIX44d[]
  Looking for: char BeginString_FIX44d[]
  Looking for: char []
  Using: %typemap(varin) char []

Finding the typemap can then be a challenge, but if you run with the -E option and analyse the output, it is quite easy to find that it comes from strings.swg. I presume MATLAB is using the UTL, so this typemap should 'come for free'. Is MATLAB not using the UTL typemaps?

jaeandersson commented 8 years ago

@wsfulton OK, thanks! I've managed to isolate the problem. The problem is that the varin and varout typemaps are never emitted.