sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Wrong userdata type detected

Open s13n opened this issue 1 year ago • 1 comments

I am struggling with a case where several different factory functions return a std::shared_ptr to different user defined types. The factory functions are implemented in C++ and bound with sol 3.3.1 to be called from Lua. The problem is that when I pass a shared_ptr thus obtained to another function as a parameter, the type of the object is reported to be wrong. This is roughly what happens:

-- Lua code:
require 'MyFactories'
local objA = makeObjA()
local objB = makeObjB(objA)    -- error here: objA is a shared_ptr to the wrong type

// C++ code (in shared object that gets loaded with the above require):
std::shared_ptr<A> makeObjA() { ... }
std::shared_ptr<B> makeObjB(std::shared_ptr<A> a) { ... }
lua["makeObjA"] = &makeObjA;
lua["makeObjB"] = &makeObjB;

The puzzling thing is that Lua complains about the parameter passed to makeObjB is some other type X that is not related to either A or B. Something like expected userdata, received sol.sol::d::u<X>: value is a userdata but is not the correct unique usertype (bad argument into 'std::shared_ptr<B>(std::shared_ptr<A>)')

Also puzzling is that the error happens with GCC on Linux, regardless of the version of GCC (I tested all versions from 10 to 14), but it doesn't happen with clang18 on the same Linux system. On Windows with MSVC 2022 it doesn't show up, either. I'm using CMake to control the build, and I use Lua 5.4.7 compiled as C++ as part of the same build.

Before I work to produce a complete test case, has somebody encountered something similar? Any hints as to how to proceed?

s13n avatar Nov 05 '24 22:11 s13n

Possibly related to #1556

deadlocklogic avatar Nov 24 '24 08:11 deadlocklogic