From b010446b17d87de83b54f431dbbbd6dd745fad1f Mon Sep 17 00:00:00 2001
From: florent <florent@laas.fr>
Date: Thu, 21 Oct 2010 21:44:52 +0200
Subject: [PATCH] Fix several memory errors

    * include/dynamic-graph/command.h,
    * include/dynamic-graph/value.h
    * src/command/command.cpp,
    * src/command/value.cpp.
---
 include/dynamic-graph/command.h |  2 +-
 include/dynamic-graph/value.h   | 25 +++++++-----
 src/command/command.cpp         |  7 +++-
 src/command/value.cpp           | 71 ++++++++++++++++++++++++++++-----
 4 files changed, 84 insertions(+), 21 deletions(-)

diff --git a/include/dynamic-graph/command.h b/include/dynamic-graph/command.h
index 3f0d02f..c8ad75e 100644
--- a/include/dynamic-graph/command.h
+++ b/include/dynamic-graph/command.h
@@ -44,7 +44,7 @@ namespace dynamicgraph {
       /// Set parameter values
       void setParameterValues(const std::vector<Value>& values);
       /// Get parameter values
-      const std::vector<Value> getParameterValues();
+      const std::vector<Value>& getParameterValues() const;
       /// Execute the command after checking parameters
       Value execute();
       /// Get a reference to the Entity owning this command
diff --git a/include/dynamic-graph/value.h b/include/dynamic-graph/value.h
index 6e151de..ab46905 100644
--- a/include/dynamic-graph/value.h
+++ b/include/dynamic-graph/value.h
@@ -35,6 +35,7 @@ namespace dynamicgraph {
 	STRING,
 	NB_TYPES
       };
+      ~Value();
       /// template constructor
       template <class T> Value(const T& value);
       /// Copy constructor
@@ -45,32 +46,38 @@ namespace dynamicgraph {
       Type type() const;
 
       /// Return the value if it is a double and throw otherwise
-      const double& doubleValue () const;
+      double doubleValue () const;
       /// Return the value if it is a int and throw otherwise
-      const int& intValue () const;
+      int intValue () const;
       /// Return the value if it is a string and throw otherwise
-      const std::string& stringValue () const;
+      std::string stringValue () const;
       /// Return the name of the type
       static std::string typeName(Type type);
+
+      /// Output in a stream
+      friend std::ostream& operator<<(std::ostream& os, const Value& value);
     private:
       Type type_;
       const void* value_;
     };
 
     // Template constructors
-    template <> Value::Value(const int& value)
+    template <> inline Value::Value(const int& value)
     {
-      value_ = &value;
+      std::cout << "Constructor of int value" << std::endl;
+      value_ = new int(value);
       type_ = INT;
     }
-    template <> Value::Value(const double& value)
+    template <> inline Value::Value(const double& value)
     {
-      value_ = &value;
+      std::cout << "Constructor of double value" << std::endl;
+      value_ = new double(value);
       type_ = DOUBLE;
     }
-    template <> Value::Value(const std::string& value)
+    template <> inline Value::Value(const std::string& value)
     {
-      value_ = &value;
+      std::cout << "Constructor of string value" << std::endl;
+      value_ = new std::string(value);
       type_ = STRING;
     }
     template <class T> Value::Value(const T& value)
diff --git a/src/command/command.cpp b/src/command/command.cpp
index e0c1745..8411c58 100644
--- a/src/command/command.cpp
+++ b/src/command/command.cpp
@@ -55,9 +55,14 @@ namespace dynamicgraph {
       }
       // Copy vector of values in private part
       valueVector_ = values;
+      std::cout << "Command::SetParameterValues" << std::endl;
+      for (unsigned int i=0; i<valueVector_.size(); i++) {
+	std::cout << ", value[" << i << "]=(" << valueVector_[i]
+		  << ")" << std::endl;
+      }
     }
 
-    const std::vector<Value> Command::getParameterValues()
+    const std::vector<Value>& Command::getParameterValues() const
     {
       return valueVector_;
     }
diff --git a/src/command/value.cpp b/src/command/value.cpp
index 06fd51d..092f0c7 100644
--- a/src/command/value.cpp
+++ b/src/command/value.cpp
@@ -21,13 +21,42 @@
 namespace dynamicgraph {
   namespace command {
 
-    Value::Value(const Value& value) : type_(value.type_),
-				       value_(value.value_)
+    Value::~Value()
     {
+      switch(type_) {
+      case INT:
+	delete (int*)value_;
+	break;
+      case DOUBLE:
+	delete (double*)value_;
+	break;
+      case STRING:
+	delete (std::string*)value_;
+	break;
+      }
+    }
+
+    Value::Value(const Value& value) : type_(value.type_)
+    {
+      switch(value.type_) {
+      case INT:
+	std::cout << "Value copy constructor: int" << std::endl;
+	value_ = new int(value.intValue());
+	break;
+      case DOUBLE:
+	std::cout << "Value copy constructor: double" << std::endl;
+	value_ = new double(value.doubleValue());
+	break;
+      case STRING:
+	std::cout << "Value copy constructor: string" << std::endl;
+	value_ = new std::string(value.stringValue());
+	break;
+      }
     }
 
     Value::Value() : type_(NONE), value_(NULL)
     {
+      std::cout << "Value empty constructor" << std::endl;
     }
 
     Value::Type Value::type() const
@@ -35,30 +64,33 @@ namespace dynamicgraph {
       return type_;
     }
 
-    const double& Value::doubleValue () const 
+    double Value::doubleValue () const
     {
+      double result;
       if (type_ == DOUBLE)
-	return *(static_cast<const double*>(value_));
+	result = *((double*)value_);
+      std::cout << "Value::doubleValue = " << result << std::endl;
+      return result;
       throw ExceptionAbstract(ExceptionAbstract::TOOLS,
 			      "value is not a double");
     }
 
-    const int& Value::intValue () const
+    int Value::intValue () const
     {
       if (type_ == INT)
-	return *(static_cast<const int*>(value_));
+	return *((int*)value_);
       throw ExceptionAbstract(ExceptionAbstract::TOOLS,
 			      "value is not an int");
     }
-    
-    const std::string& Value::stringValue () const
+
+    std::string Value::stringValue () const
     {
       if (type_ == STRING)
-	return *(static_cast<const std::string*>(value_));
+	return *((std::string*)value_);
       throw ExceptionAbstract(ExceptionAbstract::TOOLS,
 			      "value is not an string");
     }
-    
+
     std::string Value::typeName(Type type)
     {
       switch (type) {
@@ -71,5 +103,24 @@ namespace dynamicgraph {
       }
       return std::string("unknown");
     }
+
+    std::ostream& operator<<(std::ostream& os, const Value& value)
+    {
+      os << "Type=" << Value::typeName(value.type_)
+	 << ", value=";
+      switch (value.type_) {
+      case Value::INT:
+	os << value.intValue();
+	break;
+      case Value::DOUBLE:
+	os << value.doubleValue();
+	break;
+      case Value::STRING:
+	os << value.stringValue();
+	break;
+      }
+      return os;
+    }
+
   } // namespace command
 } //namespace dynamicgraph
-- 
GitLab