diff --git a/hgsane/sane_hg_mdw.cpp b/hgsane/sane_hg_mdw.cpp index 8d52db6..33ebeba 100644 --- a/hgsane/sane_hg_mdw.cpp +++ b/hgsane/sane_hg_mdw.cpp @@ -140,6 +140,11 @@ namespace local_utility return malloc(bytes); } + void free_memory(void* m) + { + if (m) + free(m); + } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1020,7 +1025,7 @@ hg_sane_middleware::~hg_sane_middleware() for (size_t i = 0; i < opts_.size(); ++i) { - free(opts_[i].desc); + local_utility::free_memory(opts_[i].desc); } for (size_t i = 0; i < openning_.size(); ++i) hg_scanner_close(openning_[i].handle, true); @@ -1379,6 +1384,26 @@ SANE_Option_Descriptor* hg_sane_middleware::number_option_to_SANE_descriptor(con return sod; } +void hg_sane_middleware::on_device_closed(scanner_handle h) +{ + // 由于目前对多设备的支持还不是刚需,故代码只考虑单设备情况,设备关闭后,清除所有变量 + for (size_t i = 0; i < opts_.size(); ++i) + { + local_utility::free_memory(opts_[i].desc); + } + opts_.clear(); + for (size_t i = 0; i < openning_.size(); ++i) + { + if (openning_[i].handle == h) + { + openning_.erase(openning_.begin() + i); + i--; + } + } + cur_vals_.clear(); + slave_options_.clear(); + master_options_.clear(); +} SANE_Status hg_sane_middleware::open(SANE_String_Const devicename, SANE_Handle* handle, const char* name, const char* pwd, const char* method, char* rsc) { scanner_handle h = NULL; @@ -1814,6 +1839,8 @@ SANE_Status hg_sane_middleware::close_device(SANE_Handle h) if (hs) err = local_utility::scanner_err_2_sane_statu(hg_scanner_close(hs, true)); + if (err == SANE_STATUS_GOOD) + on_device_closed(hs); return err; } diff --git a/hgsane/sane_hg_mdw.h b/hgsane/sane_hg_mdw.h index 35eeedf..d707cce 100644 --- a/hgsane/sane_hg_mdw.h +++ b/hgsane/sane_hg_mdw.h @@ -69,6 +69,7 @@ class hg_sane_middleware static double sane_fixed_2_double(SANE_Fixed v); static std::string option_value_2_string(SANE_Value_Type type, void* val); + void on_device_closed(scanner_handle h); SANE_Status open(SANE_String_Const devicename, SANE_Handle* handle, const char* name, const char* pwd, const char* method, char* rsc); SANE_Option_Descriptor* from_json(scanner_handle h, json* jsn, int opt_no); scanner_handle find_openning_device(SANE_Handle h, bool rmv = false, OPENDEV* dev = NULL);