In SWIG, I've got a wrapped C++ base class that is inherited in Python. The C++ side invokes callbacks on the Python derived class, which works fine. Unfortunately, one of the parameters of the callback is passed as reference. Accessing this reference yields:
Fatal unhandled exception: SWIG director method error. Error detected when calling 'Base.Start'
test.i
%module(directors="1", allprotected="1") test
%feature("director") Base;
class Base
{
public:
virtual bool Start(const Property& config) = 0; // PASSED AS REFERENCE
};
enum PropertyType{PT_REAL, PT_STRING};
class Property
{
public:
PropertyType GetType() const;
int GetSize() const;
};
In Python, Start
is correctly callback/invoked. Unfortunately, invoking GetType()
or GetSize()
yields the error above. Invoking functions of parameters passed as pointer is going fine.
import test
class PyClient(test.Base):
def Start(self, config):
config_size = config.GetSize() // YIELDS ERROR
config_type = config.GetType() // YIELDS ERROR
return True
I guess I need to convert the parameter Property from a reference to a pointer, but it is unclear to me how this works in SWIG.
UPDATE
It seems the argument in the invoked callback has a different underlying type than when created on the Python
side.
def Start(self, config):
prop_test = test.Property()
type_test = prop_test.GetType() #FINE
type_test2 = config.GetType() # ERROR
When a Property
(prop_test
) is created on the Python
side in Start()
, its type is
<mds.Property; proxy of <Swig Object of type 'Property *' at 0x000001CC24DA3D80> >
Whereas the passed property has a type
<Swig Object of type 'Base::Property *' at 0x000001CC24CBE1E0>
I wonder whether this is expected, or this might lead to the underlying issue.
Swig 4.0.2
Any help would be really appreciated.
Ben
Ah, I finally figured it out, turned out that the Property
class was in a different namespace in the C++
code, but not in the SWIG interface file.
By setting the right namespace, a proxy was generated and everything worked fine.