Fix DialogBrowse crashing when deleting the last item nullptr. In iOS use std::recursive_mutex instead of the custom check.

This commit is contained in:
2018-10-03 01:02:53 +02:00
parent c9aecf885d
commit f45eefe433
3 changed files with 65 additions and 22 deletions

View File

@@ -39,23 +39,26 @@ int t_count = 0;
glm::vec2 t_pos; glm::vec2 t_pos;
int lock_count = 0; int lock_count = 0;
NSThread* lock_thread; NSThread* lock_thread;
std::recursive_mutex lock_mutex;
@implementation GameViewController @implementation GameViewController
- (void)async_lock - (void)async_lock
{ {
if (lock_thread != [NSThread currentThread] || lock_count == 0) lock_mutex.lock();
[gl_lock lock]; // if (lock_thread != [NSThread currentThread] || lock_count == 0)
lock_thread = [NSThread currentThread]; // [gl_lock lock];
lock_count++; // lock_thread = [NSThread currentThread];
// lock_count++;
[EAGLContext setCurrentContext:self.context]; [EAGLContext setCurrentContext:self.context];
GLKView* view = (GLKView*)self.view; GLKView* view = (GLKView*)self.view;
[view bindDrawable]; [view bindDrawable];
} }
- (void)async_unlock - (void)async_unlock
{ {
lock_count--; // lock_count--;
if (lock_count == 0) // if (lock_count == 0)
[gl_lock unlock]; // [gl_lock unlock];
lock_mutex.unlock();
} }
- (void)async_swap - (void)async_swap
{ {

View File

@@ -1530,11 +1530,30 @@ void ui::Canvas::project_save_thread(std::string file_path)
// static char name[128]; // static char name[128];
// sprintf(name, "%s/latlong.ppi", data_path.c_str()); // sprintf(name, "%s/latlong.ppi", data_path.c_str());
FILE* fp = fopen(file_path.c_str(), "wb"); FILE* fp;
if (!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()); fclose(fp);
return; // 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; 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 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(&data_size, sizeof(int), 1, fp);
fwrite(compressed.data(), 1, compressed.size(), fp); fwrite(compressed.data(), 1, compressed.size(), fp);
@@ -1616,10 +1635,30 @@ void ui::Canvas::project_save_thread(std::string file_path)
} }
} }
fclose(fp); fclose(fp);
LOG("project saved to %s", file_path.c_str()); if (use_tmp)
{
// App::I.upload(data_path); LOG("project saved tmp to %s", tmp_path.c_str());
// LOG("uploaded"); 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_unsaved = false;
m_newdoc = false; m_newdoc = false;

View File

@@ -47,6 +47,8 @@ void NodeDialogBrowse::init_controls()
msgbox->btn_ok->on_click = [this,msgbox](Node*){ msgbox->btn_ok->on_click = [this,msgbox](Node*){
auto path = current->m_path; auto path = current->m_path;
int idx = container->get_child_index(current); int idx = container->get_child_index(current);
if (auto image_tex = current->find<NodeImageTexture>("thumb-tex"))
image_tex->tex.destroy();
container->remove_child(current); container->remove_child(current);
if (!container->m_children.empty()) if (!container->m_children.empty())
{ {
@@ -59,8 +61,6 @@ void NodeDialogBrowse::init_controls()
else else
{ {
current = nullptr; current = nullptr;
auto image_tex = find<NodeImageTexture>("thumb-tex");
image_tex->tex.destroy();
} }
Asset::delete_file(path); Asset::delete_file(path);
msgbox->destroy(); msgbox->destroy();
@@ -94,10 +94,11 @@ void NodeDialogBrowse::init_controls()
}; };
// load thumb // load thumb
auto image_tex = node->find<NodeImageTexture>("thumb-tex"); if (auto image_tex = node->find<NodeImageTexture>("thumb-tex"))
image_tex->tex.destroy(); {
image_tex->tex.create(thumb); image_tex->tex.destroy();
image_tex->tex.create(thumb);
}
container->add_child(node); container->add_child(node);
} }
// if (auto* first = (NodeDialogBrowseItem*)container->get_child_at(0)) // if (auto* first = (NodeDialogBrowseItem*)container->get_child_at(0))