From 9c355f131d4decceb155410a63524b9ee852d15f Mon Sep 17 00:00:00 2001 From: sol981 Date: Mon, 1 Apr 2024 20:51:10 +0700 Subject: [PATCH] fix segmetation fault error --- src/chatbot.cpp | 85 ++++++++++++++++++++++++++----------------------- src/chatbot.h | 10 +++--- 2 files changed, 51 insertions(+), 44 deletions(-) diff --git a/src/chatbot.cpp b/src/chatbot.cpp index 14bf142e..15bfdcdb 100644 --- a/src/chatbot.cpp +++ b/src/chatbot.cpp @@ -45,58 +45,65 @@ ChatBot::~ChatBot() //// STUDENT CODE //// -ChatBot::ChatBot(ChatBot& cb) -{ - std::cout << "ChatBot copy constructor" << std::endl; - - // invalidate data handles - _chatLogic = nullptr; - _rootNode = nullptr; - - // load image into heap memory - _image = new wxBitmap(); - _image = cb.GetImageHandle(); - _chatLogic = cb.GetChatLogicHandle(); -} +ChatBot::ChatBot(const ChatBot &source) { + std::cout << "ChatBot Copy Constructor" << std::endl; -ChatBot::ChatBot(ChatBot&& cb) -{ - std::cout << "ChatBot move constructor" << std::endl; - delete _image; - delete _chatLogic; + // image is owned, so we allocate the memory and copy content + _image = new wxBitmap; + *_image = *source._image; - // load image into heap memory - _image = std::move(cb.GetImageHandle()); - _chatLogic = std::move(cb.GetChatLogicHandle()); + // not-owned, so we just copy pointers + _currentNode = source._currentNode; + _rootNode = source._rootNode; + _chatLogic = source._chatLogic; } -ChatBot& ChatBot::operator=(ChatBot& cb) -{ - std::cout << "ChatBot copy assignment operator" << std::endl; - if (this == &cb) - { - return *this; +ChatBot& ChatBot::operator=(const ChatBot &source) { + std::cout << "ChatBot Copy Assignment" << std::endl; + if (this == &source) { + return *this; } - delete[] this; - _image = new wxBitmap(); - _image = cb.GetImageHandle(); - _chatLogic = cb.GetChatLogicHandle(); + delete _image; + _image = new wxBitmap; + *_image = *source._image; + + _currentNode = source._currentNode; + _rootNode = source._rootNode; + _chatLogic = source._chatLogic; return *this; } -ChatBot& ChatBot::operator=(ChatBot&& cb) -{ - std::cout << "ChatBot move assignment operator" << std::endl; - delete[] this; +ChatBot::ChatBot(ChatBot &&source) noexcept { + std::cout << "ChatBot Move Constructor" << std::endl; + _image = source._image; + _currentNode = source._currentNode; + _rootNode = source._rootNode; + _chatLogic = source._chatLogic; + + source._image = nullptr; + source._currentNode = nullptr; + source._rootNode = nullptr; + source._chatLogic = nullptr; +} + +ChatBot& ChatBot::operator=(ChatBot &&source) noexcept { + std::cout << "ChatBot Move Assignment" << std::endl; + if (this == &source) { + return *this; + } + + delete _image; + _image = new wxBitmap; + *_image = *source._image; + + _currentNode = source._currentNode; + _rootNode = source._rootNode; + _chatLogic = source._chatLogic; - _image = std::move(cb.GetImageHandle()); - _chatLogic = std::move(cb.GetChatLogicHandle()); - return *this; } - //// //// EOF STUDENT CODE diff --git a/src/chatbot.h b/src/chatbot.h index acaa6601..f5ec03f5 100644 --- a/src/chatbot.h +++ b/src/chatbot.h @@ -29,11 +29,11 @@ class ChatBot //// STUDENT CODE //// - - ChatBot(ChatBot& cb); - ChatBot(ChatBot&& cb); - ChatBot& operator=(ChatBot& cb); - ChatBot& operator=(ChatBot&& cb); + ChatBot(const ChatBot &source); + ChatBot& operator=(const ChatBot &source); + ChatBot(ChatBot &&source) noexcept; + ChatBot& operator=(ChatBot &&source) noexcept; + //// //// EOF STUDENT CODE