Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua events not propagating to event handlers outside of the wxLuaState itself #127

Open
Zekkers opened this issue Oct 11, 2023 · 6 comments
Assignees

Comments

@Zekkers
Copy link

Zekkers commented Oct 11, 2023

I recently merged up to the latest wxLua and noticed that I was no longer getting events like wxEVT_LUA_ERROR which I capture to log and display.

I checked the diff of the merge and noticed the changes from commit bde906e39b19f5179c66c44c9b681f40bf4475e0 . I reverted just that commit in my own repo, and the events started working again.

The frame in question is largely based on the wxLuaStandaloneApp with respect to the event handling.

Looks like wxDEFINE_EVENT calls in wxlstate.h could be executed multiple times if included by different modules, so the event IDs are not matching.

@pkulchenko pkulchenko self-assigned this Oct 12, 2023
@pkulchenko
Copy link
Owner

@Zekkers, thank you for the report; this is interesting. I'm not sure why it would behave this way, as there is definitely #ifndef _WXLSTATE_H_ check at the top of wxlstate.h file, so it should only be processed once. I wonder if it makes any difference for you if you move the lines in the offending change to the end of the file, as is shown in the following diff:

diff --git a/wxLua/modules/wxlua/wxlstate.h b/wxLua/modules/wxlua/wxlstate.h
index eff8a51..405edba 100644
--- a/wxLua/modules/wxlua/wxlstate.h
+++ b/wxLua/modules/wxlua/wxlstate.h
@@ -792,18 +792,6 @@ public:
     lua_Debug *m_lua_Debug;
 };
 
-#if wxCHECK_VERSION(3,0,0)
-wxDEFINE_EVENT(wxEVT_LUA_CREATION, wxLuaEvent);
-wxDEFINE_EVENT(wxEVT_LUA_PRINT, wxLuaEvent);
-wxDEFINE_EVENT(wxEVT_LUA_ERROR, wxLuaEvent);
-wxDEFINE_EVENT(wxEVT_LUA_DEBUG_HOOK, wxLuaEvent);
-#else
-DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUA_CREATION)
-DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUA_PRINT)
-DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUA_ERROR)
-DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUA_DEBUG_HOOK)
-#endif
-
 #if wxCHECK_VERSION(3,0,0)
 // A wxLuaState is being created, sent at the end of
 //   wxLuaState(wxEvtHandler, win id) or Create(wxEvtHandler, win id)
@@ -843,4 +831,16 @@ typedef void (wxEvtHandler::*wxLuaEventFunction)(wxLuaEvent&);
 #define EVT_LUA_ERROR(id, fn)      wx__DECLARE_WXLUAEVT(wxEVT_LUA_ERROR,      id, fn)
 #define EVT_LUA_DEBUG_HOOK(id, fn) wx__DECLARE_WXLUAEVT(wxEVT_LUA_DEBUG_HOOK, id, fn)
 
+#if wxCHECK_VERSION(3,0,0)
+wxDEFINE_EVENT(wxEVT_LUA_CREATION, wxLuaEvent);
+wxDEFINE_EVENT(wxEVT_LUA_PRINT, wxLuaEvent);
+wxDEFINE_EVENT(wxEVT_LUA_ERROR, wxLuaEvent);
+wxDEFINE_EVENT(wxEVT_LUA_DEBUG_HOOK, wxLuaEvent);
+#else
+DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUA_CREATION)
+DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUA_PRINT)
+DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUA_ERROR)
+DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUA_DEBUG_HOOK)
+#endif
+
 #endif // _WXLSTATE_H_

@pkulchenko pkulchenko changed the title Lua events not propogating to event handlers outside of the wxLuaState itself Lua events not propagating to event handlers outside of the wxLuaState itself Oct 12, 2023
@pkulchenko
Copy link
Owner

@Zekkers, BTW, do you have a code sample to test this (when it's working or not working)?

@pkulchenko
Copy link
Owner

@Zekkers, do you have any update on this or a sample I can use to test this? Thanks!

@Zekkers
Copy link
Author

Zekkers commented Nov 22, 2023

Sorry. I'll try and put something together tomorrow. Completely forgot about this!

@Zekkers
Copy link
Author

Zekkers commented Nov 24, 2023

So, under MSVC, trying to move the definitions to the end of the wxlstate.h file results in linker warnings for libraries, and errors for binaries.

For library linking, it comes back with warnings:

14:18:14:335	6>LuaHttpRequest.obj : warning LNK4006: "class wxEventTypeTag<class wxLuaEvent> const wxEVT_LUA_CREATION" (?wxEVT_LUA_CREATION@@3V?$wxEventTypeTag@VwxLuaEvent@@@@B) already defined in LuaHttpResponse.obj; second definition ignored
14:18:14:335	6>LuaHttpRequest.obj : warning LNK4006: "class wxEventTypeTag<class wxLuaEvent> const wxEVT_LUA_PRINT" (?wxEVT_LUA_PRINT@@3V?$wxEventTypeTag@VwxLuaEvent@@@@B) already defined in LuaHttpResponse.obj; second definition ignored
14:18:14:335	6>LuaHttpRequest.obj : warning LNK4006: "class wxEventTypeTag<class wxLuaEvent> const wxEVT_LUA_ERROR" (?wxEVT_LUA_ERROR@@3V?$wxEventTypeTag@VwxLuaEvent@@@@B) already defined in LuaHttpResponse.obj; second definition ignored
14:18:14:335	6>LuaHttpRequest.obj : warning LNK4006: "class wxEventTypeTag<class wxLuaEvent> const wxEVT_LUA_DEBUG_HOOK" (?wxEVT_LUA_DEBUG_HOOK@@3V?$wxEventTypeTag@VwxLuaEvent@@@@B) already defined in LuaHttpResponse.obj; second definition ignored

and for binary linking, comes back with:

14:23:07:407	1>wxLua.lib(wxlua_bind.obj) : error LNK2005: "class wxEventTypeTag<class wxLuaEvent> const wxEVT_LUA_CREATION" (?wxEVT_LUA_CREATION@@3V?$wxEventTypeTag@VwxLuaEvent@@@@B) already defined in App.obj
14:23:07:407	1>wxLua.lib(wxlua_bind.obj) : error LNK2005: "class wxEventTypeTag<class wxLuaEvent> const wxEVT_LUA_PRINT" (?wxEVT_LUA_PRINT@@3V?$wxEventTypeTag@VwxLuaEvent@@@@B) already defined in App.obj
14:23:07:407	1>wxLua.lib(wxlua_bind.obj) : error LNK2005: "class wxEventTypeTag<class wxLuaEvent> const wxEVT_LUA_ERROR" (?wxEVT_LUA_ERROR@@3V?$wxEventTypeTag@VwxLuaEvent@@@@B) already defined in App.obj
14:23:07:407	1>wxLua.lib(wxlua_bind.obj) : error LNK2005: "class wxEventTypeTag<class wxLuaEvent> const wxEVT_LUA_DEBUG_HOOK" (?wxEVT_LUA_DEBUG_HOOK@@3V?$wxEventTypeTag@VwxLuaEvent@@@@B) already defined in App.obj

Even though the definitions are in an include guard, including the file still implies that you're using the contents. So the compiler will still 'do' what was in there. It just doesn't have to re-parse. So, because wxDEFINE_EVENT generates a public variable wxEventTypeTag<wxLuaEvent> wxEVT_LUA_DEBUG_HOOK for example, its doing the same for every inclusion of this file.

I recompiled with the definitions before the declarations, and checked the map file. It shows multiple initialisers, apparently one for each translation unit:

0003:000007e0       ?wxEVT_LUA_CREATION$initializer$@@3P6AXXZEA 00000001428f47e0     App.obj
0003:00000a08       ?wxEVT_LUA_CREATION$initializer$@@3P6AXXZEA 00000001428f4a08     LuaConsole.obj
0003:00000a48       ?wxEVT_LUA_CREATION$initializer$@@3P6AXXZEA 00000001428f4a48     LuaInstanceWindow.obj
etc. etc.

So, to the best of my knowledge, events created in different translation units do not match the ones in the interested party.

@valid-ptr
Copy link
Contributor

This commit: bde906e breaks static linking and must be reverted.

Definitions (wxDEFINE_EVENT) must be in cpp files -> object files -> static archives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants