From 6b9556e711d78f35e0633749b8add2b6165e24eb Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 27 Mar 2021 02:57:15 +0100 Subject: [PATCH] Crash fix Two fixes really: 1. fix some parent pointers being wrong 2. fix notebooks being destroyed while node tree is still valid --- main.cpp | 7 ++++--- notebook_tree.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++--- notebook_tree.hpp | 21 ++++++++++++++++++++- 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 3e25e22..f154339 100644 --- a/main.cpp +++ b/main.cpp @@ -45,12 +45,13 @@ int main (int argc, char* argv[]) { using std::string_view; QApplication app(argc, argv); - duck::MainWindow win; - win.show(); - std::vector roots = duck::build_notebook_tree(BASE_PATH); + duck::Notebooks notebooks(BASE_PATH); + std::vector roots = notebooks.build_tree(); std::vector dele_list = duck::make_list_of_deleted(roots); + duck::MainWindow win(&roots.back()); + win.show(); for (const duck::NotebookInfo* nb : dele_list) { for (const auto& f : nb->files) { std::cout << "/home/root/.local/share/remarkable/xochitl/" << f << '\n'; diff --git a/notebook_tree.cpp b/notebook_tree.cpp index 127400f..f24be28 100644 --- a/notebook_tree.cpp +++ b/notebook_tree.cpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace fs = std::filesystem; @@ -69,6 +70,17 @@ NotebookMapType build_notebook_infos ( return retval; } +void assert_parents_correct (const Node* node) { +#if !defined(NDEBUG) + for (const auto& child : node->children) { + assert(child.parent == node); + assert_parents_correct(&child); + } +#else + static_cast(node); +#endif +} + std::vector to_tree (const NotebookMapType& nbs) { std::unordered_map> parents; for (const auto& nb : nbs) { @@ -102,15 +114,27 @@ std::vector to_tree (const NotebookMapType& nbs) { if (node.info) { auto it_parent = parents.find(node.info->parent); Node* dst_node = (parents.end() == it_parent ? grouped_nodes[node.info->parent] : &it_parent->second.second); + node.parent = dst_node; + assert(dst_node->children.capacity() > dst_node->children.size()); dst_node->children.push_back(std::move(node)); + for (auto& child : dst_node->children.back().children) { + child.parent = &dst_node->children.back(); + } grouped_nodes[dst_node->children.back().name] = &dst_node->children.back(); } else { + node.parent = nullptr; roots.push_back(std::move(node)); grouped_nodes[node.name] = &roots.back(); } } + for (auto& root : roots) { + for (auto& child : root.children) { + child.parent = &root; + } + } + return roots; } @@ -136,9 +160,24 @@ std::vector make_dele_list (const Node& tree) { } } //unnamed namespace -std::vector build_notebook_tree (std::string_view base_path) { - auto notebooks = build_notebook_infos(base_path); - return to_tree(notebooks); +struct Notebooks::LocalData { + NotebookMapType nb_map; +}; + +Notebooks::Notebooks (std::string_view base_path) : + m_local(std::make_unique()) +{ + m_local->nb_map = build_notebook_infos(base_path); +} + +Notebooks::~Notebooks() noexcept = default; + +std::vector Notebooks::build_tree() const { + auto tree = to_tree(m_local->nb_map); + for (const auto& root : tree) { + assert_parents_correct(&root); + } + return tree; //to_tree(m_local->nb_map); } std::vector make_list_of_deleted (const std::vector& roots) { diff --git a/notebook_tree.hpp b/notebook_tree.hpp index 65b7f61..31b3893 100644 --- a/notebook_tree.hpp +++ b/notebook_tree.hpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace duck { struct NotebookInfo { @@ -40,6 +41,12 @@ struct Node { parent(nullptr) { } + Node (const Node&) = delete; + Node (Node&& other) = default; + + Node& operator= (const Node&) = delete; + Node& operator= (Node&&) = default; + Node (std::string_view name, const NotebookInfo* payload, Node* parent) : name(name), info(payload), @@ -52,6 +59,18 @@ struct Node { const Node* parent; }; -std::vector build_notebook_tree (std::string_view base_path); +class Notebooks { +public: + Notebooks (std::string_view base_path); + ~Notebooks() noexcept; + + std::vector build_tree() const; + +private: + struct LocalData; + + std::unique_ptr m_local; +}; + std::vector make_list_of_deleted (const std::vector& roots); } //namespace duck