From f45eefe433159747cbddae8d3a4db51954383f40 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Wed, 3 Oct 2018 01:02:53 +0200 Subject: [PATCH] Fix DialogBrowse crashing when deleting the last item nullptr. In iOS use std::recursive_mutex instead of the custom check. --- PanoPainter/GameViewController.m | 17 ++++++---- src/canvas.cpp | 57 +++++++++++++++++++++++++++----- src/node_dialog_browse.cpp | 13 ++++---- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/PanoPainter/GameViewController.m b/PanoPainter/GameViewController.m index 1b0248c..565a590 100644 --- a/PanoPainter/GameViewController.m +++ b/PanoPainter/GameViewController.m @@ -39,23 +39,26 @@ int t_count = 0; glm::vec2 t_pos; int lock_count = 0; NSThread* lock_thread; +std::recursive_mutex lock_mutex; @implementation GameViewController - (void)async_lock { - if (lock_thread != [NSThread currentThread] || lock_count == 0) - [gl_lock lock]; - lock_thread = [NSThread currentThread]; - lock_count++; + lock_mutex.lock(); +// if (lock_thread != [NSThread currentThread] || lock_count == 0) +// [gl_lock lock]; +// lock_thread = [NSThread currentThread]; +// lock_count++; [EAGLContext setCurrentContext:self.context]; GLKView* view = (GLKView*)self.view; [view bindDrawable]; } - (void)async_unlock { - lock_count--; - if (lock_count == 0) - [gl_lock unlock]; +// lock_count--; +// if (lock_count == 0) +// [gl_lock unlock]; + lock_mutex.unlock(); } - (void)async_swap { diff --git a/src/canvas.cpp b/src/canvas.cpp index e0a8163..136c299 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -1530,11 +1530,30 @@ void ui::Canvas::project_save_thread(std::string file_path) // static char name[128]; // sprintf(name, "%s/latlong.ppi", data_path.c_str()); - FILE* fp = fopen(file_path.c_str(), "wb"); - if (!fp) + FILE* fp; + + std::string tmp_path = file_path.substr(0, file_path.size() - strlen(".ppi")) + ".tmp.ppi"; + bool use_tmp = false; + // check if file already exists + if ((fp = fopen(file_path.c_str(), "rb"))) { - LOG("cannot write project to %s", file_path.c_str()); - return; + fclose(fp); + // use tmp file for writing + use_tmp = true; + if (!(fp = fopen(tmp_path.c_str(), "wb"))) + { + LOG("cannot write project to %s", file_path.c_str()); + return; + } + } + else + { + // write directly to the new file + if (!(fp = fopen(file_path.c_str(), "wb"))) + { + LOG("cannot write project to %s", file_path.c_str()); + return; + } } PPIHeader ppi_header; @@ -1601,7 +1620,7 @@ void ui::Canvas::project_save_thread(std::string file_path) }; int ret = stbi_write_png_to_func(callback, &compressed, sz.x, sz.y, 4, snap.image[plane_index].get(), sz.x * 4); - int data_size = compressed.size(); + int data_size = (int)compressed.size(); fwrite(&data_size, sizeof(int), 1, fp); fwrite(compressed.data(), 1, compressed.size(), fp); @@ -1616,10 +1635,30 @@ void ui::Canvas::project_save_thread(std::string file_path) } } fclose(fp); - LOG("project saved to %s", file_path.c_str()); - -// App::I.upload(data_path); -// LOG("uploaded"); + if (use_tmp) + { + LOG("project saved tmp to %s", tmp_path.c_str()); + LOG("swapping to %s", file_path.c_str()); + if (std::remove(file_path.c_str()) == 0) + { + if (std::rename(tmp_path.c_str(), file_path.c_str()) == 0) + { + LOG("tmp file swapped succesfully"); + } + else + { + LOG("tmp file NOT swapped, original removed"); + } + } + else + { + LOG("could not remove %s", file_path.c_str()); + } + } + else + { + LOG("project saved to %s", file_path.c_str()); + } m_unsaved = false; m_newdoc = false; diff --git a/src/node_dialog_browse.cpp b/src/node_dialog_browse.cpp index 69d3bba..2f4e32e 100644 --- a/src/node_dialog_browse.cpp +++ b/src/node_dialog_browse.cpp @@ -47,6 +47,8 @@ void NodeDialogBrowse::init_controls() msgbox->btn_ok->on_click = [this,msgbox](Node*){ auto path = current->m_path; int idx = container->get_child_index(current); + if (auto image_tex = current->find("thumb-tex")) + image_tex->tex.destroy(); container->remove_child(current); if (!container->m_children.empty()) { @@ -59,8 +61,6 @@ void NodeDialogBrowse::init_controls() else { current = nullptr; - auto image_tex = find("thumb-tex"); - image_tex->tex.destroy(); } Asset::delete_file(path); msgbox->destroy(); @@ -94,10 +94,11 @@ void NodeDialogBrowse::init_controls() }; // load thumb - auto image_tex = node->find("thumb-tex"); - image_tex->tex.destroy(); - image_tex->tex.create(thumb); - + if (auto image_tex = node->find("thumb-tex")) + { + image_tex->tex.destroy(); + image_tex->tex.create(thumb); + } container->add_child(node); } // if (auto* first = (NodeDialogBrowseItem*)container->get_child_at(0))