fix crash where a shared lib is loaded/unloaded repeatedly

When a shared lib which uses nana is dynamic loaded/unloaded multiple
times, the crash would occur when creating a form after reloading the shared lib
This commit is contained in:
Jinhao 2019-08-14 03:39:26 +08:00
parent 15a015aa4e
commit e37cc5ec37
2 changed files with 17 additions and 4 deletions

View File

@ -173,15 +173,22 @@ namespace detail
static LRESULT WINAPI Bedrock_WIN32_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); static LRESULT WINAPI Bedrock_WIN32_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
HINSTANCE windows_module_handle()
{
MEMORY_BASIC_INFORMATION mbi;
static int dummy;
VirtualQuery(&dummy, &mbi, sizeof(mbi));
return reinterpret_cast<HINSTANCE>(mbi.AllocationBase);
}
bedrock::bedrock() bedrock::bedrock()
: pi_data_(new pi_data), : pi_data_(new pi_data),
impl_(new private_impl) impl_(new private_impl)
{ {
nana::detail::platform_spec::instance(); //to guaranty the platform_spec object is initialized before using. nana::detail::platform_spec::instance(); //to guaranty the platform_spec object is initialized before using.
WNDCLASSEX wincl; WNDCLASSEX wincl;
wincl.hInstance = ::GetModuleHandle(0); wincl.hInstance = windows_module_handle();
wincl.lpszClassName = L"NanaWindowInternal"; wincl.lpszClassName = L"NanaWindowInternal";
wincl.lpfnWndProc = &Bedrock_WIN32_WindowProc; wincl.lpfnWndProc = &Bedrock_WIN32_WindowProc;
wincl.style = CS_DBLCLKS | CS_OWNDC; wincl.style = CS_DBLCLKS | CS_OWNDC;
@ -229,6 +236,8 @@ namespace detail
delete impl_; delete impl_;
delete pi_data_; delete pi_data_;
::UnregisterClass(L"NanaWindowInternal", windows_module_handle());
} }

View File

@ -34,6 +34,10 @@ namespace nana{
namespace detail{ namespace detail{
#if defined(NANA_WINDOWS) #if defined(NANA_WINDOWS)
//This function is defined in bedrock_windows.cpp
HINSTANCE windows_module_handle();
class tray_manager class tray_manager
{ {
struct window_extra_t struct window_extra_t
@ -315,7 +319,7 @@ namespace nana{
HWND native_wd = ::CreateWindowEx(style_ex, L"NanaWindowInternal", L"Nana Window", HWND native_wd = ::CreateWindowEx(style_ex, L"NanaWindowInternal", L"Nana Window",
style, style,
pt.x, pt.y, 100, 100, pt.x, pt.y, 100, 100,
reinterpret_cast<HWND>(owner), 0, ::GetModuleHandle(0), 0); reinterpret_cast<HWND>(owner), 0, windows_module_handle(), 0);
//A window may have a border, this should be adjusted the client area fit for the specified size. //A window may have a border, this should be adjusted the client area fit for the specified size.
::RECT client; ::RECT client;
@ -504,7 +508,7 @@ namespace nana{
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS, WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_CLIPSIBLINGS,
r.x, r.y, r.width, r.height, r.x, r.y, r.width, r.height,
reinterpret_cast<HWND>(parent), // The window is a child-window to desktop reinterpret_cast<HWND>(parent), // The window is a child-window to desktop
0, ::GetModuleHandle(0), 0); 0, windows_module_handle(), 0);
#elif defined(NANA_X11) #elif defined(NANA_X11)
nana::detail::platform_scope_guard psg; nana::detail::platform_scope_guard psg;