My contribution: What was changed was changing manual new's and delete's to memory managed by smart pointers (all exclusive ownership so unique_ptr's), then using move semantics to move objects managed by smart pointers + save memory used, modifying functions to comply with Rule of 5, changing the chatbot to being declared on the stack so that when the program ends it is automatically cleaned up.
As stated in the original starter code - The ChatBot code creates a dialogue where users can ask questions about some aspects of memory management in C++. After the knowledge base of the chatbot has been loaded from a text file, a knowledge graph representation is created in computer memory, where chatbot answers represent the graph nodes and user queries represent the graph edges. After a user query has been sent to the chatbot, the Levenshtein distance is used to identify the most probable answer. The code was fully functional as-is and used raw pointers to represent the knowledge graph and interconnections between objects throughout the project.
- cmake >= 3.11
- All OSes: click here for installation instructions
- make >= 4.1 (Linux, Mac), 3.81 (Windows)
- Linux: make is installed by default on most Linux distros
- Mac: install Xcode command line tools to get make
- Windows: Click here for installation instructions
- gcc/g++ >= 5.4
- Linux: gcc / g++ is installed by default on most Linux distros
- Mac: same deal as make - install Xcode command line tools
- Windows: recommend using MinGW
- wxWidgets >= 3.0
- Linux:
sudo apt-get install libwxgtk3.0-gtk3-dev libwxgtk3.0-gtk3-0v5
. If you are facing unmet dependency issues, refer to the official page for installing the unmet dependencies. - Mac: There is a homebrew installation available.
- Installation instructions can be found here. Some version numbers may need to be changed in instructions to install v3.0 or greater.
- Linux:
- Make a build directory in the top level directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./membot
.
- Remove bug leading to crashing with segmentation fault when window closed and program called to end. This was due to a double free of resources.
- In file
chatgui.h
/chatgui.cpp
, make_chatLogic
an exclusive resource to classChatbotPanelDialog
using an appropriate smart pointer. Where required, make changes to the code such that data structures and function parameters reflect the new structure. - In file
chatbot.h
/chatbot.cpp
, make changes to the classChatBot
such that it complies with the Rule of Five. Make sure to properly allocate / deallocate memory resources on the heap and also copy member data where it makes sense to you. In each of the methods (e.g. the copy constructor), print a string of the type "ChatBot Copy Constructor" to the console so that you can see which method is called in later examples. - In file
chatlogic.h
/chatlogic.cpp
, adapt the vector_nodes
in a way that the instances ofGraphNodes
to which the vector elements refer are exclusively owned by the classChatLogic
. Use an appropriate type of smart pointer to achieve this. Where required, make changes to the code such that data structures and function parameters reflect the changes. When passing theGraphNode
instances to functions, make sure to not transfer ownership and try to contain the changes to classChatLogic
where possible. - In files
chatlogic.h
/chatlogic.cpp
andgraphnode.h
/graphnode.cpp
change the ownership of all instances ofGraphEdge
in a way such that each instance ofGraphNode
exclusively owns the outgoingGraphEdges
and holds non-owning references to incomingGraphEdges
. Use appropriate smart pointers and where required, make changes to the code such that data structures and function parameters reflect the changes. When transferring ownership from classChatLogic
, where all instances ofGraphEdge
are created, into instances ofGraphNode
, make sure to use move semantics. - In file
chatlogic.cpp
, create a localChatBot
instance on the stack at the bottom of functionLoadAnswerGraphFromFile
. Then, use move semantics to pass theChatBot
instance into the root node. Make sure thatChatLogic
has no ownership relation to theChatBot
instance and thus is no longer responsible for memory allocation and deallocation. Note that the member_chatBot
ofChatLogic
remains so it can be used as a communication handle between GUI andChatBot
instance. Make all required changes in fileschatlogic.h
/chatlogic.cpp
andgraphnode.h
/graphnode.cpp
. When the program is executed, messages on which part of the Rule of Five components ofChatBot
is called should be printed to the console. When sending a query to theChatBot
, the output should look like the following:
ChatBot Constructor
ChatBot Move Constructor
ChatBot Move Assignment Operator
ChatBot Destructor
ChatBot Destructor