Skip to content

Commit

Permalink
fix segmetation fault error
Browse files Browse the repository at this point in the history
  • Loading branch information
sol981 committed Apr 1, 2024
1 parent f968950 commit 9c355f1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
85 changes: 46 additions & 39 deletions src/chatbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions src/chatbot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9c355f1

Please sign in to comment.