From 9c49af1c2d0a54af29c4edd0e36ffc6ff8556191 Mon Sep 17 00:00:00 2001
From: Alopez <alopez@iri.upc.edu>
Date: Tue, 6 Apr 2021 16:35:14 +0200
Subject: [PATCH] Fixed exception when creating a logger with a tree not
 initialized

---
 .../iri_base_bt_client/iri_base_bt_client.h   | 28 ++++++++++++++++---
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/include/iri_base_bt_client/iri_base_bt_client.h b/include/iri_base_bt_client/iri_base_bt_client.h
index caba2ee..4b6082d 100644
--- a/include/iri_base_bt_client/iri_base_bt_client.h
+++ b/include/iri_base_bt_client/iri_base_bt_client.h
@@ -476,7 +476,12 @@ template <class ConfigClass>
 void IriBaseBTClient<ConfigClass>::enable_cout_logger()
 {
   if(this->logger_cout==NULL)
-    this->logger_cout=new BT::StdCoutLogger(this->tree);
+  {
+    if (this->tree.rootNode() != nullptr)
+      this->logger_cout=new BT::StdCoutLogger(this->tree);
+    else
+      ROS_WARN("IriBaseBTClient::enable_cout_logger -> BehaviorTree not initialized. Disable and re-enable BT cout logger if not working.");
+  }
 }
 
 template <class ConfigClass>
@@ -504,7 +509,12 @@ void IriBaseBTClient<ConfigClass>::enable_minitrace_logger()
       path_ok=true;
 
     if(path_ok)
-      this->logger_minitrace=new BT::MinitraceLogger(this->tree, (this->path + "/logs/" + this->tree_xml_file + ".json").c_str());
+    {
+      if (this->tree.rootNode() != nullptr)
+        this->logger_minitrace=new BT::MinitraceLogger(this->tree, (this->path + "/logs/" + this->tree_xml_file + ".json").c_str());
+      else
+        ROS_WARN("IriBaseBTClient::enable_minitrace_logger -> BehaviorTree not initialized. Disable and re-enable BT minitrace logger if not working.");
+    }
     else
       ROS_ERROR("IriBaseBTClient: MinitraceLogger couldn't be enabled");
   }
@@ -535,7 +545,12 @@ void IriBaseBTClient<ConfigClass>::enable_file_logger()
       path_ok=true;
 
     if(path_ok)
-      this->logger_file=new BT::FileLogger(this->tree, (this->path + "/logs/" + this->tree_xml_file + ".fbl").c_str());
+    {
+      if (this->tree.rootNode() != nullptr)
+        this->logger_file=new BT::FileLogger(this->tree, (this->path + "/logs/" + this->tree_xml_file + ".fbl").c_str());
+      else
+        ROS_WARN("IriBaseBTClient::enable_file_logger -> BehaviorTree not initialized. Disable and re-enable BT file logger if not working.");
+    }
     else
       ROS_ERROR("IriBaseBTClient: Filelogger couldn't be enabled");
   }
@@ -545,7 +560,12 @@ template <class ConfigClass>
 void IriBaseBTClient<ConfigClass>::enable_zmq_publisher()
 {
   if(this->publisher_zmq==NULL)
-    this->publisher_zmq=new BT::PublisherZMQ(this->tree);
+  {
+    if (this->tree.rootNode() != nullptr)
+      this->publisher_zmq=new BT::PublisherZMQ(this->tree);
+    else 
+      ROS_WARN("IriBaseBTClient::enable_zmq_publisher -> BehaviorTree not initialized. Disable and re-enable BT ZMQ logger if not working.");
+  }
 }
 
 template <class ConfigClass>
-- 
GitLab