From fee9a29a57db8b426a0b0f136445a16fb32773ae Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Mon, 16 Nov 2020 16:14:18 +1100 Subject: osd/modules/input: Fixed a couple of X11 resource leaks. --- src/osd/modules/input/input_x11.cpp | 51 ++++++++++++++++++++++++------------- src/osd/modules/midi/portmidi.cpp | 3 +-- src/osd/sdl/sdlmain.cpp | 33 ++++++++++++------------ 3 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/osd/modules/input/input_x11.cpp b/src/osd/modules/input/input_x11.cpp index e8888865ad5..baa8cf2f56f 100644 --- a/src/osd/modules/input/input_x11.cpp +++ b/src/osd/modules/input/input_x11.cpp @@ -267,15 +267,30 @@ public: class x11_event_manager : public event_manager_t { private: - Display * m_display; + struct x_cleanup + { + void operator()(Display *ptr) const + { + if (ptr) + XCloseDisplay(ptr); + } + void operator()(XExtensionVersion *ptr) const + { + if (ptr) + XFree(ptr); + } + }; - x11_event_manager() - : event_manager_t(), - m_display(nullptr) + template using x_ptr = std::unique_ptr; + + x_ptr m_display; + + x11_event_manager() : event_manager_t() { } + public: - Display * display() const { return m_display; } + Display * display() const { return m_display.get(); } static x11_event_manager& instance() { @@ -287,18 +302,18 @@ public: { std::lock_guard scope_lock(m_lock); - if (m_display != nullptr) + if (m_display) return 0; - m_display = XOpenDisplay(nullptr); - if (m_display == nullptr) + m_display.reset(XOpenDisplay(nullptr)); + if (!m_display) { osd_printf_verbose("Unable to connect to X server\n"); return -1; } - XExtensionVersion *version = XGetExtensionVersion(m_display, INAME); - if (!version || (version == reinterpret_cast(NoSuchExtension))) + x_ptr version(XGetExtensionVersion(m_display.get(), INAME)); + if (!version || (version.get() == reinterpret_cast(NoSuchExtension))) { osd_printf_verbose("xinput extension not available!\n"); return -1; @@ -313,21 +328,21 @@ public: XEvent xevent; // If X11 has become invalid for some reason, XPending will crash. Assert instead. - assert(m_display != nullptr); + assert(m_display); //Get XInput events - while (XPending(m_display) != 0) + while (XPending(m_display.get()) != 0) { - XNextEvent(m_display, &xevent); + XNextEvent(m_display.get(), &xevent); // Find all subscribers for the event type - auto subscribers = m_subscription_index.equal_range(xevent.type); + auto const subscribers = m_subscription_index.equal_range(xevent.type); // Dispatch the events - std::for_each(subscribers.first, subscribers.second, [&xevent](auto &pair) - { - pair.second->handle_event(xevent); - }); + std::for_each( + subscribers.first, + subscribers.second, + [&xevent] (auto &pair) { pair.second->handle_event(xevent); }); } } }; diff --git a/src/osd/modules/midi/portmidi.cpp b/src/osd/modules/midi/portmidi.cpp index 9deca98ea2d..d6bd3b0e031 100644 --- a/src/osd/modules/midi/portmidi.cpp +++ b/src/osd/modules/midi/portmidi.cpp @@ -20,8 +20,7 @@ class pm_module : public osd_module, public midi_module { public: - pm_module() - : osd_module(OSD_MIDI_PROVIDER, "pm"), midi_module() + pm_module() : osd_module(OSD_MIDI_PROVIDER, "pm"), midi_module() { } virtual ~pm_module() { } diff --git a/src/osd/sdl/sdlmain.cpp b/src/osd/sdl/sdlmain.cpp index 37d1434a2e8..5e0b12cef3b 100644 --- a/src/osd/sdl/sdlmain.cpp +++ b/src/osd/sdl/sdlmain.cpp @@ -440,25 +440,25 @@ void sdl_osd_interface::init(running_machine &machine) osd_setenv(SDLENV_VIDEODRIVER, stemp, 1); } - stemp = options().render_driver(); - if (stemp != nullptr) + stemp = options().render_driver(); + if (stemp != nullptr) + { + if (strcmp(stemp, OSDOPTVAL_AUTO) != 0) + { + osd_printf_verbose("Setting SDL renderdriver '%s' ...\n", stemp); + //osd_setenv(SDLENV_RENDERDRIVER, stemp, 1); + SDL_SetHint(SDL_HINT_RENDER_DRIVER, stemp); + } + else { - if (strcmp(stemp, OSDOPTVAL_AUTO) != 0) - { - osd_printf_verbose("Setting SDL renderdriver '%s' ...\n", stemp); - //osd_setenv(SDLENV_RENDERDRIVER, stemp, 1); - SDL_SetHint(SDL_HINT_RENDER_DRIVER, stemp); - } - else - { #if defined(SDLMAME_WIN32) - // OpenGL renderer has less issues with mode switching on windows - osd_printf_verbose("Setting SDL renderdriver '%s' ...\n", "opengl"); - //osd_setenv(SDLENV_RENDERDRIVER, stemp, 1); - SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); + // OpenGL renderer has less issues with mode switching on windows + osd_printf_verbose("Setting SDL renderdriver '%s' ...\n", "opengl"); + //osd_setenv(SDLENV_RENDERDRIVER, stemp, 1); + SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); #endif - } } + } /* Set the SDL environment variable for drivers wanting to load the * lib at startup. @@ -491,7 +491,8 @@ void sdl_osd_interface::init(running_machine &machine) /* Initialize SDL */ - if (SDL_InitSubSystem(SDL_INIT_VIDEO)) { + if (SDL_InitSubSystem(SDL_INIT_VIDEO)) + { osd_printf_error("Could not initialize SDL %s\n", SDL_GetError()); exit(-1); } -- cgit v1.2.3