Skip to content

Commit

Permalink
Memory leak exists in proxy model
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaMKW committed Sep 13, 2024
1 parent ffc72eb commit 59534ee
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 77 deletions.
1 change: 1 addition & 0 deletions include/gui/project/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace Toolbox::UI {
[[nodiscard]] bool onLoadData(const std::filesystem::path& path) override {
m_project_root = path;
m_file_system_model = make_referable<FileSystemModel>();
m_file_system_model->initialize();
m_file_system_model->setRoot(m_project_root);
m_tree_proxy.setSourceModel(m_file_system_model);
m_tree_proxy.setDirsOnly(true);
Expand Down
10 changes: 9 additions & 1 deletion include/model/fsmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace Toolbox {
FileSystemModel() = default;
~FileSystemModel() = default;

void initialize();

[[nodiscard]] UUID64 getUUID() const override { return m_uuid; }

[[nodiscard]] const fs_path &getRoot() const &;
Expand Down Expand Up @@ -138,6 +140,7 @@ namespace Toolbox {
UUID64 m_root_index;

mutable std::unordered_map<UUID64, ModelIndex> m_index_map;
mutable std::unordered_map<std::string, RefPtr<const ImageHandle>> m_icon_map;
};

class FileSystemModelSortFilterProxy : public IDataModel {
Expand Down Expand Up @@ -206,13 +209,15 @@ namespace Toolbox {
[[nodiscard]] bool canFetchMore(const ModelIndex &index);
void fetchMore(const ModelIndex &index);

protected:
[[nodiscard]] ModelIndex toSourceIndex(const ModelIndex &index) const;
[[nodiscard]] ModelIndex toProxyIndex(const ModelIndex &index) const;

protected:
[[nodiscard]] ModelIndex toProxyIndex(int64_t row, int64_t column,
const ModelIndex &parent = ModelIndex()) const;

[[nodiscard]] bool isFiltered(const UUID64 &uuid) const;
void cacheIndex(const ModelIndex &index) const;

ModelIndex makeIndex(const fs_path &path, int64_t row, const ModelIndex &parent) {
return ModelIndex();
Expand All @@ -227,6 +232,9 @@ namespace Toolbox {
std::string m_filter = "";

bool m_dirs_only = false;

mutable std::unordered_map<UUID64, bool> m_filter_map;
mutable std::unordered_map<UUID64, std::vector<int64_t>> m_row_map;
};

} // namespace Toolbox
25 changes: 15 additions & 10 deletions src/gui/project/window.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "gui/project/window.hpp"
#include "model/fsmodel.hpp"

#include <imgui/imgui.h>
#include <cmath>
#include <imgui/imgui.h>

namespace Toolbox::UI {

Expand Down Expand Up @@ -30,15 +30,15 @@ namespace Toolbox::UI {
}

void ProjectViewWindow::renderProjectFolderView() {
if (!m_view_proxy.validateIndex(m_view_index)) {
if (!m_file_system_model->validateIndex(m_view_index)) {
return;
}

if (ImGui::BeginChild("Folder View", {0, 0}, true,
ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoDecoration)) {
if (m_view_proxy.hasChildren(m_view_index)) {
if (m_view_proxy.canFetchMore(m_view_index)) {
m_view_proxy.fetchMore(m_view_index);
if (m_file_system_model->hasChildren(m_view_index)) {
if (m_file_system_model->canFetchMore(m_view_index)) {
m_file_system_model->fetchMore(m_view_index);
}

ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {2, 2});
Expand All @@ -53,16 +53,16 @@ namespace Toolbox::UI {
x_count = 1;
}

for (size_t i = 0; i < m_view_proxy.getRowCount(m_view_index); ++i) {
ModelIndex child_index = m_view_proxy.getIndex(i, 0, m_view_index);
for (size_t i = 0; i < m_file_system_model->getRowCount(m_view_index); ++i) {
ModelIndex child_index = m_file_system_model->getIndex(i, 0, m_view_index);

if (ImGui::BeginChild(child_index.getUUID(), {76, 92}, true,
ImGuiWindowFlags_ChildWindow |
ImGuiWindowFlags_NoDecoration)) {

m_icon_painter.render(*m_view_proxy.getDecoration(child_index), {72, 72});
m_icon_painter.render(*m_file_system_model->getDecoration(child_index), {72, 72});

std::string text = m_view_proxy.getDisplayText(child_index);
std::string text = m_file_system_model->getDisplayText(child_index);
ImVec2 text_size = ImGui::CalcTextSize(text.c_str());

ImVec2 pos = ImGui::GetCursorScreenPos();
Expand All @@ -71,7 +71,7 @@ namespace Toolbox::UI {

ImGui::RenderTextEllipsis(
ImGui::GetWindowDrawList(), pos, pos + ImVec2(64, 20), pos.x + 64.0f,
pos.x + 76.0f, m_view_proxy.getDisplayText(child_index).c_str(),
pos.x + 76.0f, m_file_system_model->getDisplayText(child_index).c_str(),
nullptr, nullptr);
}
ImGui::EndChild();
Expand Down Expand Up @@ -113,6 +113,11 @@ namespace Toolbox::UI {
}
is_open = ImGui::TreeNodeEx(m_tree_proxy.getDisplayText(index).c_str(),
ImGuiTreeNodeFlags_OpenOnArrow);

if (ImGui::IsItemClicked()) {
m_view_index = m_tree_proxy.toSourceIndex(index);
}

if (is_open) {
for (size_t i = 0; i < m_tree_proxy.getRowCount(index); ++i) {
ModelIndex child_index = m_tree_proxy.getIndex(i, 0, index);
Expand Down
1 change: 1 addition & 0 deletions src/image/imagehandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ namespace Toolbox {
}

void ImageHandle::moveGL(ImageHandle &&image) {
unloadGL();
m_image_handle = image.m_image_handle;
m_image_format = image.m_image_format;
m_image_width = image.m_image_width;
Expand Down
Loading

0 comments on commit 59534ee

Please sign in to comment.