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

Accessing data in std::vector<double> #75

Open ianhbell opened 7 years ago

ianhbell commented 7 years ago

In my C++ code, I sometimes return std::vector. That end up getting wrapped in a DoubleVector from the swig typemap, but for the life of me, I can't figure out how to get access to the data being wrapped. I tried to pop(), but that only works once, and the second call crashes MATLAB. The at() function isn't wrapped, and neither is any other type of accessor. So, how do I get my data back out of a DoubleVector?

jaeandersson commented 7 years ago

Sounds like it's not implemented properly. In CasADi we have a output typemap for it. So the user never works directly with it - it gets converted to a MATLAB row vector.

ianhbell commented 7 years ago

Well, it does seem that you can index into the DoubleVector using x(3) for instance, but I keep getting random crashes that I can't debug. I return a struct that holds std::vector and that seems to add more wrinkles to the vector wrapping.

jaeandersson commented 7 years ago

OK, it's probably a reference counting thing. It needs to keep a reference to the original vector<double> in order to do things like mystruct.x(3) = 3.

ianhbell commented 7 years ago

How fixable is this? At the moment I have a non-swig-based MATLAB wrapper working, so it's not the end of the world, but it is quite annoying that I can can't have vectors in returned structs.

jaeandersson commented 7 years ago

It's probably fixable with a moderate effort. But someone has to do it.