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;
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
{

View File

@@ -1530,12 +1530,31 @@ 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")))
{
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;
fwrite(&ppi_header, sizeof(PPIHeader), 1, fp);
@@ -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);
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());
// App::I.upload(data_path);
// LOG("uploaded");
}
m_unsaved = false;
m_newdoc = false;

View File

@@ -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<NodeImageTexture>("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<NodeImageTexture>("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<NodeImageTexture>("thumb-tex");
if (auto image_tex = node->find<NodeImageTexture>("thumb-tex"))
{
image_tex->tex.destroy();
image_tex->tex.create(thumb);
}
container->add_child(node);
}
// if (auto* first = (NodeDialogBrowseItem*)container->get_child_at(0))