kaguya
kaguya copied to clipboard
Wrong object lifetime in move operators
I have a loop like this:
struct Foo{ int bar; Foo(int bar):bar(bar){}};
for(int i=0; i<5; i++){
Foo foo = Foo(i);
state["Foo" + std::to_string(i)] = foo;
}
If I then access the state["Foo2"] I get wrong results because kaguya stores a pointer to the local foo
instance.
I traced it a bit (VS) and found, that it goes through the move assignment operator which finally stores a pointer to the value, instead of the value.
Changing the line to state["Foo" + std::to_string(i)] = static_cast<Foo const&>(foo);
fixes this. However the examples suggest, that both should be the same.
Do I misunderstand this or is this a bug?
thanks. It's bug
Thanks. Could you probably add some tests that check the expected garbage collection is working? Cases I could think of:
- Value, const Value, const Value&, Value&& -> Copy and gc after re-assignment
- Pointer Value and Value& -> No gc and original instance (in C++) will be modified
- Reassign temporary (e.g. from array), then pass into lua should also behave correctly (see above loop, due to a bug "Foo0" could be equal to "Foo4")
These would be a bit higher level tests (they test multiple parts of the library together) but these can detect regression bugs. Probably also candiates for function calls (const, value and ref arguments should behave correctly)