Fix Crash when using m_ProfileJson (#79)

* Recover: Packaging Process

* Update: Flutter 1222

* Fix: crash when recreate GUI

* Fix Crash when using m_profile_json
This commit is contained in:
xiaoyeliu
2025-12-22 16:24:48 +08:00
committed by GitHub
parent f6899b277f
commit f729c53b20
4 changed files with 161 additions and 91 deletions

View File

@@ -33,12 +33,14 @@
#include <libslic3r/miniz_extension.hpp>
#include <libslic3r/Utils.hpp>
#include "CreatePresetsDialog.hpp"
#include <mutex>
using namespace nlohmann;
namespace Slic3r { namespace GUI {
json m_ProfileJson;
std::mutex m_ProfileJson_mutex;
static wxString update_custom_filaments()
{
@@ -229,7 +231,11 @@ wxString GuideFrame::SetStartPage(GuidePage startpage, bool load)
} else if (startpage == BBL_FILAMENTS) {
SetTitle(_L("Setup Wizard"));
int nSize = m_ProfileJson["model"].size();
int nSize;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
nSize = m_ProfileJson["model"].size();
}
if (nSize>0)
TargetUrl = from_u8((boost::filesystem::path(resources_dir()) / "web/guide/0/index.html?target=22").make_preferred().string());
@@ -403,7 +409,10 @@ void GuideFrame::OnScriptMessage(wxWebViewEvent &evt)
json m_Res = json::object();
m_Res["command"] = "response_userguide_profile";
m_Res["sequence_id"] = "10001";
m_Res["response"] = m_ProfileJson;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
m_Res["response"] = m_ProfileJson;
}
//wxString strJS = wxString::Format("HandleStudio(%s)", m_Res.dump(-1, ' ', false, json::error_handler_t::ignore));
wxString strJS = wxString::Format("HandleStudio(%s)", m_Res.dump(-1, ' ', true));
@@ -426,43 +435,53 @@ void GuideFrame::OnScriptMessage(wxWebViewEvent &evt)
{
json MSelected = j["data"];
int nModel = m_ProfileJson["model"].size();
for (int m = 0; m < nModel; m++) {
json TmpModel = m_ProfileJson["model"][m];
m_ProfileJson["model"][m]["nozzle_selected"] = "";
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
int nModel = m_ProfileJson["model"].size();
for (int m = 0; m < nModel; m++) {
json TmpModel = m_ProfileJson["model"][m];
m_ProfileJson["model"][m]["nozzle_selected"] = "";
for (auto it = MSelected.begin(); it != MSelected.end(); ++it) {
json OneSelect = it.value();
for (auto it = MSelected.begin(); it != MSelected.end(); ++it) {
json OneSelect = it.value();
wxString s1 = TmpModel["model"];
wxString s2 = OneSelect["model"];
if (s1.compare(s2) == 0) {
m_ProfileJson["model"][m]["nozzle_selected"] = OneSelect["nozzle_diameter"];
break;
wxString s1 = TmpModel["model"];
wxString s2 = OneSelect["model"];
if (s1.compare(s2) == 0) {
m_ProfileJson["model"][m]["nozzle_selected"] = OneSelect["nozzle_diameter"];
break;
}
}
}
}
}
else if (strCmd == "save_userguide_filaments") {
//reset
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it)
{
m_ProfileJson["filament"][it.key()]["selected"] = 0;
}
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
//reset
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it)
{
m_ProfileJson["filament"][it.key()]["selected"] = 0;
}
json fSelected = j["data"]["filament"];
int nF = fSelected.size();
for (int m = 0; m < nF; m++)
{
std::string fName = fSelected[m];
json fSelected = j["data"]["filament"];
int nF = fSelected.size();
for (int m = 0; m < nF; m++)
{
std::string fName = fSelected[m];
m_ProfileJson["filament"][fName]["selected"] = 1;
m_ProfileJson["filament"][fName]["selected"] = 1;
}
}
}
else if (strCmd == "user_guide_finish") {
SaveProfile();
std::string oldregion = m_ProfileJson["region"];
std::string oldregion;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
oldregion = m_ProfileJson["region"];
}
bool bLogin = false;
if (m_Region != oldregion) {
AppConfig* config = GUI::wxGetApp().app_config;
@@ -492,7 +511,10 @@ void GuideFrame::OnScriptMessage(wxWebViewEvent &evt)
this->Close();
} else if (strCmd == "save_region") {
m_Region = j["region"];
m_ProfileJson["region"] = m_Region;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
m_ProfileJson["region"] = m_Region;
}
}
else if (strCmd == "common_openurl") {
@@ -635,7 +657,11 @@ int GuideFrame::SaveProfile()
m_MainPtr->app_config->save();
std::string strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
std::string strAll;
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
}
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << "before save to app_config: "<< std::endl<<strAll;
@@ -643,9 +669,12 @@ int GuideFrame::SaveProfile()
const std::string &section_name = AppConfig::SECTION_FILAMENTS;
std::map<std::string, std::string> section_new;
m_appconfig_new.clear_section(section_name);
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it) {
if (it.value()["selected"] == 1){
section_new[it.key()] = "true";
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
for (auto it = m_ProfileJson["filament"].begin(); it != m_ProfileJson["filament"].end(); ++it) {
if (it.value()["selected"] == 1){
section_new[it.key()] = "true";
}
}
}
m_appconfig_new.set_section(section_name, section_new);
@@ -653,7 +682,9 @@ int GuideFrame::SaveProfile()
//set vendors to app_config
Slic3r::AppConfig::VendorMap empty_vendor_map;
m_appconfig_new.set_vendors(empty_vendor_map);
for (auto it = m_ProfileJson["model"].begin(); it != m_ProfileJson["model"].end(); ++it)
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
for (auto it = m_ProfileJson["model"].begin(); it != m_ProfileJson["model"].end(); ++it)
{
if (it.value().is_object()) {
json temp_model = it.value();
@@ -997,6 +1028,7 @@ int GuideFrame::GetFilamentInfo( std::string VendorDirectory, json & pFilaList,
int GuideFrame::LoadProfileData()
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
try {
m_ProfileJson = json::parse("{}");
m_ProfileJson["model"] = json::array();
@@ -1068,7 +1100,7 @@ int GuideFrame::LoadProfileData()
}
//sync to web
std::string strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore);
std::string strAll = m_ProfileJson.dump(-1, ' ', false, json::error_handler_t::ignore); // Already locked at function start
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ", finished, json contents: " << std::endl << strAll;
json m_Res = json::object();
@@ -1093,6 +1125,7 @@ int GuideFrame::LoadProfileData()
int GuideFrame::SaveProfileData()
{
std::lock_guard<std::mutex> lock(m_ProfileJson_mutex);
try {
const auto enabled_filaments = wxGetApp().app_config->has_section(AppConfig::SECTION_FILAMENTS) ? wxGetApp().app_config->get_section(AppConfig::SECTION_FILAMENTS) : std::map<std::string, std::string>();
m_appconfig_new.set_vendors(*wxGetApp().app_config);