From 3b8773940a2929098196201db71e09f8e5dcbf2e Mon Sep 17 00:00:00 2001 From: Paul Rouget Date: Wed, 5 Aug 2020 16:33:27 +0200 Subject: [PATCH] Keep C++ pref value in memory longer --- .../hololens/ServoApp/ServoControl/Servo.cpp | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) 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 {