changeset 29547:60af3f38f4b1

use std::unique_ptr to manage interpreter object in application object * octave.h, octave.cc (application::m_interpreter): Use std::unique_ptr instead of bare pointer. Update all uses.
author John W. Eaton <jwe@octave.org>
date Wed, 21 Apr 2021 11:43:23 -0400
parents 58e1230b9503
children f6ad83cbe3c4
files libinterp/octave.cc libinterp/octave.h
diffstat 2 files changed, 12 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave.cc	Tue Apr 20 16:54:51 2021 -0400
+++ b/libinterp/octave.cc	Wed Apr 21 11:43:23 2021 -0400
@@ -296,6 +296,14 @@
     init ();
   }
 
+  // Note: Although the application destructor doesn't explicitly
+  // perform any actions, it can't be declared "default" in the header
+  // file if the octave::interpreter is an incomplete type.  Providing
+  // an explicit definition of the destructor here is much simpler than
+  // including the full declaration of octave::interpreter in the
+  // octave.h header file.
+  application::~application (void) { }
+
   void
   application::set_program_names (const std::string& pname)
   {
@@ -336,15 +344,6 @@
             ? instance->m_options.experimental_terminal_widget () : false);
   }
 
-  application::~application (void)
-  {
-    // Delete interpreter if it still exists.
-
-    delete m_interpreter;
-
-    instance = nullptr;
-  }
-
   bool application::interpreter_initialized (void)
   {
     return m_interpreter ? m_interpreter->initialized () : false;
@@ -353,7 +352,7 @@
   interpreter& application::create_interpreter (void)
   {
     if (! m_interpreter)
-      m_interpreter = new interpreter (this);
+      m_interpreter = std::unique_ptr<interpreter> (new interpreter (this));
 
     return *m_interpreter;
   }
@@ -371,9 +370,7 @@
 
   void application::delete_interpreter (void)
   {
-    delete m_interpreter;
-
-    m_interpreter = nullptr;
+    m_interpreter.reset ();
   }
 
   void application::init (void)
--- a/libinterp/octave.h	Tue Apr 20 16:54:51 2021 -0400
+++ b/libinterp/octave.h	Wed Apr 21 11:43:23 2021 -0400
@@ -29,6 +29,7 @@
 #include "octave-config.h"
 
 #include <list>
+#include <memory>
 #include <string>
 
 #include "str-vec.h"
@@ -364,7 +365,7 @@
     // from eval without persist.
     bool m_is_octave_program = false;
 
-    interpreter *m_interpreter = nullptr;
+    std::unique_ptr<interpreter> m_interpreter;
   };
 
   class OCTINTERP_API cli_application : public application