diff --git a/support/hololens/ServoApp/ServoControl/Servo.cpp b/support/hololens/ServoApp/ServoControl/Servo.cpp index 4e3eeedf981..80a445468e5 100644 --- a/support/hololens/ServoApp/ServoControl/Servo.cpp +++ b/support/hololens/ServoApp/ServoControl/Servo.cpp @@ -185,9 +185,18 @@ Servo::Servo(std::optional initUrl, hstring args, GLsizei width, std::vector cprefs; + // Ensure few things stay in memories long enough as we send raw + // pointers to Rust. + std::vector> memChar; + std::vector> memBool; + std::vector> memInt; + std::vector> memDouble; + for (auto pref : prefs.Values()) { - auto key = *hstring2char(pref.Key()); + auto charkey = hstring2char(pref.Key()); + auto key = *charkey.get(); + memChar.push_back(std::move(charkey)); auto value = pref.Value(); auto type = value.as().Type(); @@ -197,20 +206,24 @@ Servo::Servo(std::optional initUrl, hstring args, GLsizei width, cpref.value = NULL; if (type == Windows::Foundation::PropertyType::Boolean) { cpref.pref_type = capi::CPrefType::Bool; - auto val = unbox_value(value); - cpref.value = &val; + auto val = std::make_unique(unbox_value(value)); + cpref.value = val.get(); + memBool.push_back(std::move(val)); } else if (type == Windows::Foundation::PropertyType::String) { cpref.pref_type = capi::CPrefType::Str; - auto val = unbox_value(value); - cpref.value = *hstring2char(val); + auto val = hstring2char(unbox_value(value)); + cpref.value = *val.get(); + memChar.push_back(std::move(val)); } else if (type == Windows::Foundation::PropertyType::Int64) { cpref.pref_type = capi::CPrefType::Int; - auto val = unbox_value(value); - cpref.value = &val; + auto val = std::make_unique(unbox_value(value)); + cpref.value = val.get(); + memInt.push_back(std::move(val)); } else if (type == Windows::Foundation::PropertyType::Double) { cpref.pref_type = capi::CPrefType::Float; - auto val = unbox_value(value); - cpref.value = &val; + auto val = std::make_unique(unbox_value(value)); + cpref.value = val.get(); + memDouble.push_back(std::move(val)); } else if (type == Windows::Foundation::PropertyType::Empty) { cpref.pref_type = capi::CPrefType::Missing; } else {