From 789b06b1eac1d4024b49b95f3a4de7023ae00182 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Sat, 30 Jul 2022 11:44:03 -0700 Subject: [PATCH] gen-zam: fix memory management for vector-of-strings operations --- tools/gen-zam/src/Gen-ZAM.cc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tools/gen-zam/src/Gen-ZAM.cc b/tools/gen-zam/src/Gen-ZAM.cc index f9b05d7f47..11386e5cc0 100644 --- a/tools/gen-zam/src/Gen-ZAM.cc +++ b/tools/gen-zam/src/Gen-ZAM.cc @@ -1390,9 +1390,24 @@ void ZAM_ExprOpTemplate::InstantiateEval(const vector& ot_orig, if ( ! is_none && ! is_default && find_type_info(ei.LHS_ET()).is_managed && ! HasExplicitResultType() ) { - auto delim = zc == ZIC_VEC ? "->" : "."; - auto pre = "auto hold_lhs = " + lhs + delim + "ManagedVal();\n\t"; + auto pre = "auto hold_lhs = " + lhs; + + if ( zc == ZIC_VEC ) + // For vectors, we have to check for whether + // the previous value is present, or a hole. + pre += string(" ? ") + lhs + "->"; + else + pre += "."; + + pre += "ManagedVal()"; + + if ( zc == ZIC_VEC ) + pre += " : nullptr"; + + pre += ";\n\t"; + auto post = "\tUnref(hold_lhs);"; + eval = pre + eval + post; }