+
+ /* Constructor. */
+ option_def_and_value (const option_def &option_, void *ctx_,
+ gdb::optional<option_value> &&value_ = {})
+ : option (option_),
+ ctx (ctx_),
+ value (std::move (value_))
+ {
+ clear_value (option_, value_);
+ }
+
+ /* Move constructor. Need this because for some types the values
+ are allocated on the heap. */
+ option_def_and_value (option_def_and_value &&rval)
+ : option (rval.option),
+ ctx (rval.ctx),
+ value (std::move (rval.value))
+ {
+ clear_value (rval.option, rval.value);
+ }
+
+ DISABLE_COPY_AND_ASSIGN (option_def_and_value);
+
+ ~option_def_and_value ()
+ {
+ if (value.has_value ())
+ {
+ if (option.type == var_string)
+ xfree (value->string);
+ }
+ }
+
+private:
+
+ /* Clear the option_value, without releasing it. This is used after
+ the value has been moved to some other option_def_and_value
+ instance. This is needed because for some types the value is
+ allocated on the heap, so we must clear the pointer in the
+ source, to avoid a double free. */
+ static void clear_value (const option_def &option,
+ gdb::optional<option_value> &value)
+ {
+ if (value.has_value ())
+ {
+ if (option.type == var_string)
+ value->string = nullptr;
+ }
+ }