add run_get_message; print memory leaks at exit on x86-Debug

This commit is contained in:
gb 2023-11-22 12:10:22 +08:00
parent bf7282fd22
commit 5c87f8c2ad
9 changed files with 109 additions and 19 deletions

View File

@ -131,10 +131,10 @@ hg_scanner::hg_scanner(ONLNSCANNER* dev, imgproc_mgr* imgproc, hguser* user, std
init(); init();
if (status() == SCANNER_ERR_OK) if (status() == SCANNER_ERR_OK)
{ {
imgproc_ = imgproc; //imgproc_ = imgproc;
imgproc_->add_ref(); //imgproc_->add_ref();
//dev_opts_->add(imgproc_);
dev_opts_->add(imgproc_);
if (constopts) if (constopts)
{ {
for(auto& v: *constopts) for(auto& v: *constopts)
@ -162,6 +162,9 @@ shared_memory* hg_scanner::create_device_singleton(int vid, int pid, int addr)
void hg_scanner::init(void) void hg_scanner::init(void)
{ {
std::string opts("");
int ret = SCANNER_ERR_OK;
auto on_status = [this](uint32_t statu) ->void auto on_status = [this](uint32_t statu) ->void
{ {
//status_ = statu; //status_ = statu;
@ -189,18 +192,20 @@ void hg_scanner::init(void)
scanner_ = new scanner_handler(); scanner_ = new scanner_handler();
scanner_->set_image_receiver(on_image_received); scanner_->set_image_receiver(on_image_received);
scanner_->set_status_notifyer(on_status); scanner_->set_status_notifyer(on_status);
ret = scanner_->open_usb_scanner(dev_.dev);
std::string opts("");
int ret = scanner_->option_get_all(opts);
if (ret == SCANNER_ERR_OK) if (ret == SCANNER_ERR_OK)
{ {
ret = scanner_->option_get_all(opts);
set_opt_json_text(&opts[0]); set_opt_json_text(&opts[0]);
if(user_) if(user_)
dev_opts_ = new device_option(privilege, logmsg); dev_opts_ = new device_option(privilege, logmsg);
else else
dev_opts_ = new device_option(privilege_empty, logmsg); dev_opts_ = new device_option(privilege_empty, logmsg);
dev_opts_->add(this); dev_opts_->add(this);
status_ = SCANNER_ERR_OK;
msg_ = "OK";
} }
else else
{ {
@ -212,11 +217,11 @@ void hg_scanner::thread_image_processor(void)
imgproc_mgr * processor = imgproc_; imgproc_mgr * processor = imgproc_;
img_receiver* raw = nullptr; img_receiver* raw = nullptr;
processor->add_ref(); //processor->add_ref();
while (raw_imgs_.take(raw, true)) while (raw_imgs_.take(raw, true))
{ {
} }
processor->release(); //processor->release();
} }
int hg_scanner::start(void) int hg_scanner::start(void)
@ -266,6 +271,7 @@ int hg_scanner::close(void)
} }
// wait image thread ... // wait image thread ...
raw_imgs_.trigger();
if (imgpr_thread_.get() && imgpr_thread_->joinable()) if (imgpr_thread_.get() && imgpr_thread_->joinable())
imgpr_thread_->join(); imgpr_thread_->join();

View File

@ -364,6 +364,9 @@ void async_usb_host::stop_worker_threads(void)
if(t.get() && t->joinable()) \ if(t.get() && t->joinable()) \
t->join(); t->join();
out_que_.trigger();
in_que_.trigger();
WAIT_THREAD(thread_w_); WAIT_THREAD(thread_w_);
WAIT_THREAD(thread_r_); WAIT_THREAD(thread_r_);
WAIT_THREAD(thread_p_); WAIT_THREAD(thread_p_);

View File

@ -290,7 +290,7 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v
char model[40], char model[40],
vendor[40]; vendor[40];
SANE_Device_Ex de; SANE_Device_Ex de;
int ev_ui = 0, addr = 0; int ev_ui = 0;
scanner_handle h = NULL; scanner_handle h = NULL;
unsigned int len = sizeof(de); unsigned int len = sizeof(de);
std::string name(""), type(""); std::string name(""), type("");
@ -303,7 +303,6 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v
de.family = NULL; de.family = NULL;
de.vendor = vendor; de.vendor = vendor;
de.openned = SANE_FALSE; de.openned = SANE_FALSE;
addr = libusb_get_device_address(device);
if (ev == USB_EVENT_DEVICE_ARRIVED) if (ev == USB_EVENT_DEVICE_ARRIVED)
{ {
@ -367,7 +366,7 @@ void hg_scanner_mgr::on_hgscanner_pnp(usb_event ev, libusb_device* device, int v
ols.vid = vid; ols.vid = vid;
ols.pid = pid; ols.pid = pid;
ols.addr = addr; ols.addr = libusb_get_device_address(device);
ols.dev = device; ols.dev = device;
ols.scanner = nullptr; ols.scanner = nullptr;
ols.family = g_supporting_devices[index].family; ols.family = g_supporting_devices[index].family;

View File

@ -438,10 +438,9 @@ extern "C"
#if defined(WIN32) || defined(_WIN64) #if defined(WIN32) || defined(_WIN64)
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
{ {
//static _CrtMemState state;
if (reason == DLL_PROCESS_ATTACH) if (reason == DLL_PROCESS_ATTACH)
{ {
//if (g_scanner_path.empty()) //if (g_scanner_path.empty())
@ -457,6 +456,13 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
//} //}
#ifdef _DEBUG #ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetBreakAlloc(-1);
#endif
}
else if (reason == DLL_PROCESS_DETACH)
{
#ifdef _DEBUG
OutputDebugStringA("\r\nscanner module unloading ...\r\n");
#endif #endif
} }

View File

@ -185,14 +185,13 @@ BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
//_CrtMemCheckpoint(&state); //_CrtMemCheckpoint(&state);
#ifdef _DEBUG #ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetBreakAlloc(632555577); _CrtSetBreakAlloc(-1);
set_gbjson_life_callback(record_json, nullptr);
#endif #endif
} }
else if (reason == DLL_PROCESS_DETACH) else if (reason == DLL_PROCESS_DETACH)
{ {
#ifdef _DEBUG #ifdef _DEBUG
_CrtDumpMemoryLeaks(); OutputDebugStringA("\r\nsane module unloading ...\r\n");
#endif #endif
//_CrtMemDumpAllObjectsSince(&state); //_CrtMemDumpAllObjectsSince(&state);
} }

View File

@ -54,6 +54,7 @@ int gettimeofday(TIMEV* tv, struct timezone* tz)
return 0; return 0;
} }
#else #else
#include <map> #include <map>
@ -1128,7 +1129,27 @@ namespace utils
return 0; return 0;
} }
#if OS_WIN
bool run_get_message(HWND hwnd, UINT filter_min, UINT filter_max, std::function<bool(MSG*, bool*)> msghandler)
{
MSG msg = { 0 };
BOOL ret = GetMessageW(&msg, hwnd, filter_min, filter_max);
bool handled = false;
if ((DWORD)ret == -1 || !ret)
return false;
if (msghandler)
ret = msghandler(&msg, &handled) == true;
if (!handled)
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return ret == TRUE;
}
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <deque> #include <deque>
#include <functional>
enum log_type enum log_type
{ {
@ -66,6 +67,23 @@ namespace utils
int copy_log_file_to(const char* dst); int copy_log_file_to(const char* dst);
int clear_log_file(void); int clear_log_file(void);
#if OS_WIN
// Function: pump message recycle (GetMessageW)
//
// Parameters: hwnd - target window
//
// filter_min - the minimal message
//
// filter_max - the maximal message
//
// msghandler - custom message processor
// MSG*, received message by GetMessage
// bool*, return whether user handled the message, no TranslateMessage and DispatchMessage if this was true
//
// Return: whether quit the message recycle
bool run_get_message(HWND hwnd, UINT filter_min = 0, UINT filter_max = 0, std::function<bool(MSG*, bool*)> msghandler = std::function<bool(MSG*, bool*)>());
#endif
template<typename ... Args> template<typename ... Args>
void to_log(int level, const char* fmt, Args ... args) void to_log(int level, const char* fmt, Args ... args)
{ {
@ -141,6 +159,7 @@ struct timezone
int tz_dsttime; /* Nonzero if DST is ever in effect. */ int tz_dsttime; /* Nonzero if DST is ever in effect. */
}; };
int gettimeofday(TIMEV* tv, struct timezone* tz); int gettimeofday(TIMEV* tv, struct timezone* tz);
#else #else
#include <sys/time.h> #include <sys/time.h>

View File

@ -276,6 +276,30 @@ int32_t gb_json::release()
return ref; return ref;
} }
static void check_cJSON(cJSON* root, std::vector<cJSON*>* existing)
{
if (!root)
return;
for (auto& v : *existing)
{
if (v == root)
{
printf("cJSON* 0x%08x is repeat!\n", v);
break;
}
}
existing->push_back(root);
check_cJSON(root->child, existing);
cJSON* next = root->next;
while (next)
{
check_cJSON(next, existing);
next = next->next;
}
}
bool gb_json::attach_text(char* json_txt) bool gb_json::attach_text(char* json_txt)
{ {
clear(); clear();
@ -283,10 +307,21 @@ bool gb_json::attach_text(char* json_txt)
cJSON* jsn = cJSON_Parse(json_txt); cJSON* jsn = cJSON_Parse(json_txt);
if (jsn) if (jsn)
{ {
//char *text = cJSON_Print(jsn); char *text = cJSON_Print(jsn);
//if (text) if (text)
// free(text); {
if (0)
{
FILE* dst = fopen("e:\\test-json.txt", "wb");
fwrite(text, 1, strlen(text), dst);
fclose(dst);
}
free(text);
}
std::vector<cJSON*> repeat;
if(0)
check_cJSON(jsn, &repeat);
from_cjson(jsn->child); from_cjson(jsn->child);
cJSON_Delete(jsn); cJSON_Delete(jsn);

View File

@ -902,6 +902,8 @@ COM_API_IMPLEMENT(scanner, int, start(void))
thread_starting_->join(); thread_starting_->join();
#ifdef START_SCAN_IN_THREAD #ifdef START_SCAN_IN_THREAD
thread_starting_.reset(new std::thread(&scanner::thread_start, this)); thread_starting_.reset(new std::thread(&scanner::thread_start, this));
get_scanned_images(-1); // block until image arrived or scan finished
ret = err_;
#else #else
ret = thread_start(); ret = thread_start();
#endif #endif