I have a std::vector<CameraT>
which I've already bound to Python thanks to Flexo's response for SWIG and C++ memory leak with vector of pointers. But I need to access fields like CameraT.t and I can't find a proper way to do it.
It is defined in a 3rd party header file (DataInterface.h) as:
template<class FT>
struct CameraT_ {
typedef FT float_t;
float_t t[3];
/* more elaborate c++ stuff */
};
typedef CameraT_<float> CameraT;
The interface file looks roughly like this:
%module nvm
%{
#define SWIG_FILE_WITH_INIT
#include "util.h"
%}
%include "std_vector.i"
%inline %{
bool CameraDataFromNVM(const char* name, std::vector<CameraT>& camera_data){
/* wrapper code */
}
%}
%template(CamVector) std::vector<CameraT>;
And the integration test:
import nvm
if __name__ == "__main__":
datafile = '../../example-data/vidstills.nvm'
cams = nvm.CamVector()
assert nvm.CameraDataFromNVM(datafile, cams)
print(cams[0]) # this yields no memory warnings
print(cams[0].t[0]) # 'SwigPyObject' object has no attribute 't'
# Yields: swig/python detected a memory leak of type 'CameraT *', no destructor found.
c = list([i for i in cams])
prints this:
$ python3 test_nvm.py |& head
<Swig Object of type 'CameraT *' at 0x7f638228ee40>
Loading cameras/points: ../../example-data/vidstills.nvm
329 cameras; 8714 3D points; 74316 projections
Traceback (most recent call last):
File "test_nvm.py", line 20, in <module>
print(cams[0].t[0])
AttributeError: 'SwigPyObject' object has no attribute 't'
swig/python detected a memory leak of type 'CameraT *', no destructor found.
I've tried to place struct CameraT {}
and #include
statements in and outside the %inline block and failed. Also looping over the vector elements yields the memory leak warning. No idea what else to do.
The code is on Github.