Fix double delete of the Value in Struct::get_field

This commit is contained in:
Olivier Goffart 2021-03-18 14:20:30 +01:00
parent c6740fe592
commit a3d0f3155d
2 changed files with 8 additions and 10 deletions

View file

@ -185,13 +185,10 @@ public:
Type type() const { return cbindgen_private::sixtyfps_interpreter_value_type(&inner); } Type type() const { return cbindgen_private::sixtyfps_interpreter_value_type(&inner); }
// internal
Value(const sixtyfps::cbindgen_private::ValueOpaque &inner) : inner(inner) { }
private: private:
using ValueOpaque = sixtyfps::cbindgen_private::ValueOpaque; using ValueOpaque = sixtyfps::cbindgen_private::ValueOpaque;
ValueOpaque inner; ValueOpaque inner;
friend class Struct; friend struct Struct;
}; };
inline Value::Value(const sixtyfps::SharedVector<Value> &array) inline Value::Value(const sixtyfps::SharedVector<Value> &array)
@ -273,7 +270,7 @@ inline std::optional<Value> Struct::get_field(std::string_view name) const
name.size() name.size()
}; };
if (auto *value = cbindgen_private::sixtyfps_interpreter_struct_get_field(&inner, name_view)) { if (auto *value = cbindgen_private::sixtyfps_interpreter_struct_get_field(&inner, name_view)) {
return *value; return *reinterpret_cast<const Value *>(value);
} else { } else {
return {}; return {};
} }
@ -287,4 +284,4 @@ inline void Struct::set_field(std::string_view name, const Value &value)
cbindgen_private::sixtyfps_interpreter_struct_set_field(&inner, name_view, &value.inner); cbindgen_private::sixtyfps_interpreter_struct_set_field(&inner, name_view, &value.inner);
} }
} }

View file

@ -164,11 +164,12 @@ SCENARIO("Struct API")
REQUIRE(!struc.get_field("not_there")); REQUIRE(!struc.get_field("not_there"));
struc.set_field("field_a", Value(true)); struc.set_field("field_a", Value(sixtyfps::SharedString("Hallo")));
auto value_opt = struc.get_field("field_a"); auto value_opt = struc.get_field("field_a");
REQUIRE(value_opt.has_value()); REQUIRE(value_opt.has_value());
auto value = value_opt.value(); auto value = value_opt.value();
REQUIRE(value.to_bool().has_value()); REQUIRE(value.to_string().has_value());
REQUIRE(value.to_bool().value() == true); REQUIRE(value.to_string().value() == "Hallo");
}
}