Crash fix

Two fixes really:
1. fix some parent pointers being wrong
2. fix notebooks being destroyed while node tree is still valid
This commit is contained in:
King_DuckZ 2021-03-27 02:57:15 +01:00
parent fe7c5c5bc1
commit 6b9556e711
3 changed files with 66 additions and 7 deletions

View file

@ -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<duck::Node> roots = duck::build_notebook_tree(BASE_PATH);
duck::Notebooks notebooks(BASE_PATH);
std::vector<duck::Node> roots = notebooks.build_tree();
std::vector<const duck::NotebookInfo*> 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';

View file

@ -21,6 +21,7 @@
#include <stdexcept>
#include <filesystem>
#include <simdjson.h>
#include <cassert>
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<void>(node);
#endif
}
std::vector<Node> to_tree (const NotebookMapType& nbs) {
std::unordered_map<std::string_view, std::pair<unsigned int, Node>> parents;
for (const auto& nb : nbs) {
@ -102,15 +114,27 @@ std::vector<Node> 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<const NotebookInfo*> make_dele_list (const Node& tree) {
}
} //unnamed namespace
std::vector<Node> 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<LocalData>())
{
m_local->nb_map = build_notebook_infos(base_path);
}
Notebooks::~Notebooks() noexcept = default;
std::vector<Node> 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<const NotebookInfo*> make_list_of_deleted (const std::vector<Node>& roots) {

View file

@ -21,6 +21,7 @@
#include <cstdint>
#include <string>
#include <string_view>
#include <memory>
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<Node> build_notebook_tree (std::string_view base_path);
class Notebooks {
public:
Notebooks (std::string_view base_path);
~Notebooks() noexcept;
std::vector<Node> build_tree() const;
private:
struct LocalData;
std::unique_ptr<LocalData> m_local;
};
std::vector<const NotebookInfo*> make_list_of_deleted (const std::vector<Node>& roots);
} //namespace duck