From 754d180e1d0e68f4d58fd17dfd721341ace48f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Sat, 25 Jan 2025 12:07:58 -0600 Subject: [PATCH 01/18] fixed double-delete improper memory management bug --- src/graphnode.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/graphnode.cpp b/src/graphnode.cpp index 65f56060b..ca694a155 100644 --- a/src/graphnode.cpp +++ b/src/graphnode.cpp @@ -11,7 +11,6 @@ GraphNode::~GraphNode() //// STUDENT CODE //// - delete _chatBot; //// //// EOF STUDENT CODE From 39910d05075a0227e3d139580aba23a9bfbc5c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 11:02:57 -0600 Subject: [PATCH 02/18] made _chatLogic an exclusive resource to class ChatbotPanelDialog using smart unique pointer. --- src/chatgui.cpp | 4 +--- src/chatgui.h | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/chatgui.cpp b/src/chatgui.cpp index 6637e562b..ff04a5dc5 100644 --- a/src/chatgui.cpp +++ b/src/chatgui.cpp @@ -118,7 +118,7 @@ ChatBotPanelDialog::ChatBotPanelDialog(wxWindow *parent, wxWindowID id) //// // create chat logic instance - _chatLogic = new ChatLogic(); + _chatLogic = std::make_unique(); // pass pointer to chatbot dialog so answers can be displayed in GUI _chatLogic->SetPanelDialogHandle(this); @@ -135,8 +135,6 @@ ChatBotPanelDialog::~ChatBotPanelDialog() //// STUDENT CODE //// - delete _chatLogic; - //// //// EOF STUDENT CODE } diff --git a/src/chatgui.h b/src/chatgui.h index 503c59790..257cfb54c 100644 --- a/src/chatgui.h +++ b/src/chatgui.h @@ -1,6 +1,6 @@ #ifndef CHATGUI_H_ #define CHATGUI_H_ - +#include #include class ChatLogic; // forward declaration @@ -16,7 +16,7 @@ class ChatBotPanelDialog : public wxScrolledWindow //// STUDENT CODE //// - ChatLogic *_chatLogic; + std::unique_ptr _chatLogicPtr; //// //// EOF STUDENT CODE From fc21c32dc1802349bca02d7aaa4247a7bcd61518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 11:21:28 -0600 Subject: [PATCH 03/18] defined special member functions for the rule of five copy and move semantics --- src/chatbot.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/chatbot.h b/src/chatbot.h index 0367a93f8..9cf6ec1a5 100644 --- a/src/chatbot.h +++ b/src/chatbot.h @@ -29,7 +29,10 @@ class ChatBot //// STUDENT CODE //// - + ChatBot(const ChatBot &source); + ChatBot &operator=(const ChatBot &source); + ChatBot(ChatBot &&source); + ChatBot &operator=(ChatBot &&source); //// //// EOF STUDENT CODE From 56340fcb1880efabb952c5bd9257d80165d9a437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 11:49:18 -0600 Subject: [PATCH 04/18] implemented chatbot copy constructor --- src/chatbot.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index 41d1f0c1f..2727958a3 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -44,7 +44,16 @@ ChatBot::~ChatBot() //// STUDENT CODE //// - +ChatBot::ChatBot(const ChatBot &source) +{ + std::cout << "ChatBot Copy Constructor" << std::endl; + if(source._image != nullptr) image = new wxBitmap(*source._image); + else _image = nullptr; + + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; +} //// //// EOF STUDENT CODE From bfb8d427be469d67a3b399bb6f55251513ef61f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 12:02:57 -0600 Subject: [PATCH 05/18] added build and vscode folders --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index c129b08a5..44f5f7c83 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .github/** +.vscode/** +build/** + From 1dc79bb13162e492a49c6b774055d2b5d91c59c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 12:03:31 -0600 Subject: [PATCH 06/18] implemented the chatbot copy assignment operator --- src/chatbot.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index 2727958a3..8745ec470 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -54,6 +54,21 @@ ChatBot::ChatBot(const ChatBot &source) _rootNode = source._rootNode; _currentNode = source._currentNode; } + +ChatBot &ChatBot::operator=(const ChatBot &source) +{ + std::cout << "ChatBot Copy Assignment Operator" << std::endl; + + if(this == &source) return *this; + if(_image != nullptr) {delete _image; _image = nullptr;} + if(source._image != nullptr) image = new wxBitmap(*source._image); + else _image = nullptr; + + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; + return *this; +} //// //// EOF STUDENT CODE From 47d78af1fe50e88f5fc14697e73d032dd2113c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 12:19:22 -0600 Subject: [PATCH 07/18] implemented chatbot move assignment operator --- src/chatbot.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index 8745ec470..c104948db 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -69,6 +69,20 @@ ChatBot &ChatBot::operator=(const ChatBot &source) _currentNode = source._currentNode; return *this; } + +ChatBot::ChatBot(ChatBot &&source) +{ + std::cout << "ChatBot Move Constructor" << std::endl; + _image = source._image; + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; + + source._image = nullptr; + source._chatLogic = nullptr; + source._rootNode = nullptr; + source._currentNode = nullptr; +} //// //// EOF STUDENT CODE From b072f920f87d3e3bf5f97d1546b14aa01e6fbd68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 12:25:12 -0600 Subject: [PATCH 08/18] Revert "implemented the chatbot copy assignment operator" This reverts commit 1dc79bb13162e492a49c6b774055d2b5d91c59c2. --- src/chatbot.cpp | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index c104948db..8745ec470 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -69,20 +69,6 @@ ChatBot &ChatBot::operator=(const ChatBot &source) _currentNode = source._currentNode; return *this; } - -ChatBot::ChatBot(ChatBot &&source) -{ - std::cout << "ChatBot Move Constructor" << std::endl; - _image = source._image; - _chatLogic = source._chatLogic; - _rootNode = source._rootNode; - _currentNode = source._currentNode; - - source._image = nullptr; - source._chatLogic = nullptr; - source._rootNode = nullptr; - source._currentNode = nullptr; -} //// //// EOF STUDENT CODE From 75eb1cee428e56875752c8859051ec4a6f10b8a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 12:26:25 -0600 Subject: [PATCH 09/18] implemented chatbot move contructor --- src/chatbot.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index 8745ec470..c104948db 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -69,6 +69,20 @@ ChatBot &ChatBot::operator=(const ChatBot &source) _currentNode = source._currentNode; return *this; } + +ChatBot::ChatBot(ChatBot &&source) +{ + std::cout << "ChatBot Move Constructor" << std::endl; + _image = source._image; + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; + + source._image = nullptr; + source._chatLogic = nullptr; + source._rootNode = nullptr; + source._currentNode = nullptr; +} //// //// EOF STUDENT CODE From 6e8abe7c7149d0aa8b6d78f4d4212ae8fac264f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 12:30:25 -0600 Subject: [PATCH 10/18] implemented chatbot move assignment operator. --- src/chatbot.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index c104948db..ac126564e 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -83,6 +83,18 @@ ChatBot::ChatBot(ChatBot &&source) source._rootNode = nullptr; source._currentNode = nullptr; } + +ChatBot &ChatBot::operator=(ChatBot &&source) +{ + std::cout << "ChatBot Move Assignment Operator" << std::endl; + if(this == &source) return *this; + if(_image != nullptr) {delete _image; _image = nullptr;} + _image = source._image; + _chatLogic = source._chatLogic; + _rootNode = source._rootNode; + _currentNode = source._currentNode; + return *this; +} //// //// EOF STUDENT CODE From 09f6c1aa313602d56611f14272dba5d8333194cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 12:54:37 -0600 Subject: [PATCH 11/18] fixed typo 'image' instead of '_image' --- src/chatbot.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index ac126564e..267e15f1e 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -47,7 +47,7 @@ ChatBot::~ChatBot() ChatBot::ChatBot(const ChatBot &source) { std::cout << "ChatBot Copy Constructor" << std::endl; - if(source._image != nullptr) image = new wxBitmap(*source._image); + if(source._image != nullptr) _image = new wxBitmap(*source._image); else _image = nullptr; _chatLogic = source._chatLogic; @@ -61,7 +61,7 @@ ChatBot &ChatBot::operator=(const ChatBot &source) if(this == &source) return *this; if(_image != nullptr) {delete _image; _image = nullptr;} - if(source._image != nullptr) image = new wxBitmap(*source._image); + if(source._image != nullptr) _image = new wxBitmap(*source._image); else _image = nullptr; _chatLogic = source._chatLogic; From 2bfeb1278a72fc9775e2af2fa53c377ee3a8415c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 12:55:17 -0600 Subject: [PATCH 12/18] fixed typo '_chatLogicPtr' instead of '_chatLogic' --- src/chatgui.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chatgui.h b/src/chatgui.h index 257cfb54c..32c7b9b18 100644 --- a/src/chatgui.h +++ b/src/chatgui.h @@ -16,7 +16,7 @@ class ChatBotPanelDialog : public wxScrolledWindow //// STUDENT CODE //// - std::unique_ptr _chatLogicPtr; + std::unique_ptr _chatLogic; //// //// EOF STUDENT CODE From 0f81936a7f859a5e8d9f1502a4404ea53adf526f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 13:27:28 -0600 Subject: [PATCH 13/18] updated getChatLogic getter handle to return _chatlogic.get instead of the raw _chatlogic that was before the smart pointer. --- src/chatgui.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chatgui.h b/src/chatgui.h index 32c7b9b18..e71bb2879 100644 --- a/src/chatgui.h +++ b/src/chatgui.h @@ -27,7 +27,7 @@ class ChatBotPanelDialog : public wxScrolledWindow ~ChatBotPanelDialog(); // getter / setter - ChatLogic *GetChatLogicHandle() { return _chatLogic; } + ChatLogic *GetChatLogicHandle() { return _chatLogic.get(); } // events void paintEvent(wxPaintEvent &evt); From 4c356fea3083597801999c4179e489135d5a6429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 13:41:45 -0600 Subject: [PATCH 14/18] replaced vector of raw pointers to nodes for unique_ptr smart pointer to node --- src/chatlogic.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/chatlogic.h b/src/chatlogic.h index e70b0713e..18cc43ea0 100644 --- a/src/chatlogic.h +++ b/src/chatlogic.h @@ -4,6 +4,7 @@ #include #include #include "chatgui.h" +#include // forward declarations class ChatBot; @@ -17,7 +18,7 @@ class ChatLogic //// // data handles (owned) - std::vector _nodes; + std::vector> _nodes; std::vector _edges; //// From ac5eb05c7fc631e6bb8d64b6e416c654d1669f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Mon, 27 Jan 2025 19:32:33 -0600 Subject: [PATCH 15/18] ChatLogic has exclusive ownership of GraphNode objects via unique_ptr --- src/chatlogic.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/chatlogic.cpp b/src/chatlogic.cpp index 79c58ef41..a92922cc6 100644 --- a/src/chatlogic.cpp +++ b/src/chatlogic.cpp @@ -35,12 +35,6 @@ ChatLogic::~ChatLogic() // delete chatbot instance delete _chatBot; - // delete all nodes - for (auto it = std::begin(_nodes); it != std::end(_nodes); ++it) - { - delete *it; - } - // delete all edges for (auto it = std::begin(_edges); it != std::end(_edges); ++it) { @@ -127,12 +121,19 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) //// // check if node with this ID exists already - auto newNode = std::find_if(_nodes.begin(), _nodes.end(), [&id](GraphNode *node) { return node->GetID() == id; }); + auto newNode = std::find_if( + _nodes.begin(), + _nodes.end(), + [&id](std::unique_ptr &node) { + return node->GetID() == id; + } + ); + // create new element if ID does not yet exist if (newNode == _nodes.end()) { - _nodes.emplace_back(new GraphNode(id)); + _nodes.emplace_back(std::make_unique(id)); newNode = _nodes.end() - 1; // get iterator to last element // add all answers to current node @@ -156,15 +157,14 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) if (parentToken != tokens.end() && childToken != tokens.end()) { // get iterator on incoming and outgoing node via ID search - auto parentNode = std::find_if(_nodes.begin(), _nodes.end(), [&parentToken](GraphNode *node) { return node->GetID() == std::stoi(parentToken->second); }); - auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](GraphNode *node) { return node->GetID() == std::stoi(childToken->second); }); + auto parentNode = std::find_if(_nodes.begin(), _nodes.end(), [&parentToken](std::unique_ptr &node) { return node->GetID() == std::stoi(parentToken->second); }); + auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](std::unique_ptr &node) { return node->GetID() == std::stoi(childToken->second); }); // create new edge GraphEdge *edge = new GraphEdge(id); - edge->SetChildNode(*childNode); - edge->SetParentNode(*parentNode); _edges.push_back(edge); - + edge->SetChildNode(childNode->get()); + edge->SetParentNode(parentNode->get()); // find all keywords for current node AddAllTokensToElement("KEYWORD", tokens, *edge); @@ -198,15 +198,15 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) // identify root node GraphNode *rootNode = nullptr; - for (auto it = std::begin(_nodes); it != std::end(_nodes); ++it) + for (auto &node : _nodes) { // search for nodes which have no incoming edges - if ((*it)->GetNumberOfParents() == 0) + if (node->GetNumberOfParents() == 0) { if (rootNode == nullptr) { - rootNode = *it; // assign current node to root + rootNode = node.get(); // assign current node to root } else { From cf586a078581b60f028d42a7cd9c4c31bab9e8ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Tue, 28 Jan 2025 13:03:13 -0600 Subject: [PATCH 16/18] changed the ownership of all instances of GraphEdge in a way that each instance of graphNode exlusively owns the outgoing graph edges and holds non-owning references to incoming GraphEdges --- src/chatlogic.cpp | 15 ++++++--------- src/chatlogic.h | 2 +- src/graphnode.cpp | 8 +++++--- src/graphnode.h | 8 ++++---- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/chatlogic.cpp b/src/chatlogic.cpp index a92922cc6..3f163e9d1 100644 --- a/src/chatlogic.cpp +++ b/src/chatlogic.cpp @@ -35,11 +35,8 @@ ChatLogic::~ChatLogic() // delete chatbot instance delete _chatBot; - // delete all edges - for (auto it = std::begin(_edges); it != std::end(_edges); ++it) - { - delete *it; - } + // No need to manually delete edges now. + // The child nodes' unique_ptr will handle that. //// //// EOF STUDENT CODE @@ -161,16 +158,16 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](std::unique_ptr &node) { return node->GetID() == std::stoi(childToken->second); }); // create new edge - GraphEdge *edge = new GraphEdge(id); - _edges.push_back(edge); + auto edge = std::make_unique(id); + // _edges.push_back(edge); edge->SetChildNode(childNode->get()); edge->SetParentNode(parentNode->get()); // find all keywords for current node AddAllTokensToElement("KEYWORD", tokens, *edge); // store reference in child node and parent node - (*childNode)->AddEdgeToParentNode(edge); - (*parentNode)->AddEdgeToChildNode(edge); + (*childNode)->AddEdgeToParentNode(edge.get()); + (*parentNode)->AddEdgeToChildNode(std::move(edge)); } //// diff --git a/src/chatlogic.h b/src/chatlogic.h index 18cc43ea0..787f59cf2 100644 --- a/src/chatlogic.h +++ b/src/chatlogic.h @@ -19,7 +19,7 @@ class ChatLogic // data handles (owned) std::vector> _nodes; - std::vector _edges; + // std::vector _edges; //// //// EOF STUDENT CODE diff --git a/src/graphnode.cpp b/src/graphnode.cpp index ca694a155..d620e10ad 100644 --- a/src/graphnode.cpp +++ b/src/graphnode.cpp @@ -11,6 +11,8 @@ GraphNode::~GraphNode() //// STUDENT CODE //// + // We do NOT delete _parentEdges here, since they are non‐owning! + // The unique_ptr in _childEdges will automatically free the GraphEdges. //// //// EOF STUDENT CODE @@ -26,9 +28,9 @@ void GraphNode::AddEdgeToParentNode(GraphEdge *edge) _parentEdges.push_back(edge); } -void GraphNode::AddEdgeToChildNode(GraphEdge *edge) +void GraphNode::AddEdgeToChildNode(std::unique_ptr edge) { - _childEdges.push_back(edge); + _childEdges.push_back(std::move(edge)); } //// STUDENT CODE @@ -52,7 +54,7 @@ GraphEdge *GraphNode::GetChildEdgeAtIndex(int index) //// STUDENT CODE //// - return _childEdges[index]; + return _childEdges[index].get(); //// //// EOF STUDENT CODE diff --git a/src/graphnode.h b/src/graphnode.h index ba3910d20..238879e6f 100644 --- a/src/graphnode.h +++ b/src/graphnode.h @@ -4,7 +4,7 @@ #include #include #include "chatbot.h" - +#include // forward declarations class GraphEdge; @@ -16,7 +16,7 @@ class GraphNode //// // data handles (owned) - std::vector _childEdges; // edges to subsequent nodes + std::vector> _childEdges; // edges to subsequent nodes // data handles (not owned) std::vector _parentEdges; // edges to preceding nodes @@ -37,14 +37,14 @@ class GraphNode // getter / setter int GetID() { return _id; } int GetNumberOfChildEdges() { return _childEdges.size(); } + int GetNumberOfParents() { return _parentEdges.size(); } GraphEdge *GetChildEdgeAtIndex(int index); std::vector GetAnswers() { return _answers; } - int GetNumberOfParents() { return _parentEdges.size(); } // proprietary functions void AddToken(std::string token); // add answers to list void AddEdgeToParentNode(GraphEdge *edge); - void AddEdgeToChildNode(GraphEdge *edge); + void AddEdgeToChildNode(std::unique_ptr edge); //// STUDENT CODE //// From e13bf39ba781b9d797c5e04c561664d8483c1e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Thu, 30 Jan 2025 19:43:28 -0600 Subject: [PATCH 17/18] implemented task 5 in theory, it should work, but right now throws seg fault error, further debug is needed. --- src/chatlogic.cpp | 20 +++++++++++--------- src/graphnode.cpp | 10 +++++----- src/graphnode.h | 4 ++-- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/chatlogic.cpp b/src/chatlogic.cpp index 3f163e9d1..087918be0 100644 --- a/src/chatlogic.cpp +++ b/src/chatlogic.cpp @@ -18,10 +18,10 @@ ChatLogic::ChatLogic() //// // create instance of chatbot - _chatBot = new ChatBot("../images/chatbot.png"); + //_chatBot = new ChatBot("../images/chatbot.png"); // add pointer to chatlogic so that chatbot answers can be passed on to the GUI - _chatBot->SetChatLogicHandle(this); + //_chatBot->SetChatLogicHandle(this); //// //// EOF STUDENT CODE @@ -33,7 +33,7 @@ ChatLogic::~ChatLogic() //// // delete chatbot instance - delete _chatBot; + //delete _chatBot; // No need to manually delete edges now. // The child nodes' unique_ptr will handle that. @@ -195,15 +195,15 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) // identify root node GraphNode *rootNode = nullptr; - for (auto &node : _nodes) + for (auto it = std::begin(_nodes); it != std::end(_nodes); ++it) { // search for nodes which have no incoming edges - if (node->GetNumberOfParents() == 0) + if ((*it)->GetNumberOfParents() == 0) { if (rootNode == nullptr) { - rootNode = node.get(); // assign current node to root + rootNode = (*it).get(); // assign current node to root } else { @@ -211,10 +211,12 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename) } } } - + ChatBot chatbot("../images/chatbot.png"); + SetChatbotHandle(&chatbot); // add chatbot to graph root node - _chatBot->SetRootNode(rootNode); - rootNode->MoveChatbotHere(_chatBot); + chatbot.SetChatLogicHandle(this); + chatbot.SetRootNode(rootNode); + rootNode->MoveChatbotHere(std::move(chatbot)); //// //// EOF STUDENT CODE diff --git a/src/graphnode.cpp b/src/graphnode.cpp index d620e10ad..ee215a131 100644 --- a/src/graphnode.cpp +++ b/src/graphnode.cpp @@ -35,16 +35,16 @@ void GraphNode::AddEdgeToChildNode(std::unique_ptr edge) //// STUDENT CODE //// -void GraphNode::MoveChatbotHere(ChatBot *chatbot) +void GraphNode::MoveChatbotHere(ChatBot chatbot) { - _chatBot = chatbot; - _chatBot->SetCurrentNode(this); + _chatBot = std::move(chatbot); + _chatBot.SetCurrentNode(this); } void GraphNode::MoveChatbotToNewNode(GraphNode *newNode) { - newNode->MoveChatbotHere(_chatBot); - _chatBot = nullptr; // invalidate pointer at source + newNode->MoveChatbotHere(std::move(_chatBot)); + //_chatBot = nullptr; // invalidate pointer at source } //// //// EOF STUDENT CODE diff --git a/src/graphnode.h b/src/graphnode.h index 238879e6f..612f8c8e2 100644 --- a/src/graphnode.h +++ b/src/graphnode.h @@ -20,7 +20,7 @@ class GraphNode // data handles (not owned) std::vector _parentEdges; // edges to preceding nodes - ChatBot *_chatBot; + ChatBot _chatBot; //// //// EOF STUDENT CODE @@ -49,7 +49,7 @@ class GraphNode //// STUDENT CODE //// - void MoveChatbotHere(ChatBot *chatbot); + void MoveChatbotHere(ChatBot chatbot); //// //// EOF STUDENT CODE From ed7c6a7856aec1e7b1657c9ee275f8e0171c81af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gary=20Vladimir=20N=C3=BA=C3=B1ez=20L=C3=B3pez?= Date: Fri, 31 Jan 2025 12:07:21 -0600 Subject: [PATCH 18/18] fixed segfault by adding _chatLogic->SetChatbotHandle(this) --- src/chatbot.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index 267e15f1e..be7116375 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -77,7 +77,7 @@ ChatBot::ChatBot(ChatBot &&source) _chatLogic = source._chatLogic; _rootNode = source._rootNode; _currentNode = source._currentNode; - + _chatLogic->SetChatbotHandle(this); source._image = nullptr; source._chatLogic = nullptr; source._rootNode = nullptr; @@ -93,9 +93,13 @@ ChatBot &ChatBot::operator=(ChatBot &&source) _chatLogic = source._chatLogic; _rootNode = source._rootNode; _currentNode = source._currentNode; + _chatLogic->SetChatbotHandle(this); + source._image = nullptr; + source._chatLogic = nullptr; + source._rootNode = nullptr; + source._currentNode = nullptr; return *this; } -//// //// EOF STUDENT CODE void ChatBot::ReceiveMessageFromUser(std::string message)