DolphinWX: Clean up some wxTimer code

Technically fixes a memory leak (which wouldn't matter because the timer is only created once and destroyed on shutdown).

Also starting and stopping the timer does not cause leaks.
This commit is contained in:
Lioncash 2015-02-28 20:25:50 -05:00
parent 4fd980e6d4
commit 4cc19ac926
4 changed files with 13 additions and 23 deletions

View file

@ -508,15 +508,13 @@ CFrame::CFrame(wxFrame* parent,
// check if game is running // check if game is running
m_bHotkeysInit = InitControllers(); m_bHotkeysInit = InitControllers();
m_poll_hotkey_timer = new wxTimer(this); m_poll_hotkey_timer.SetOwner(this);
Bind(wxEVT_TIMER, &CFrame::PollHotkeys, this); Bind(wxEVT_TIMER, &CFrame::PollHotkeys, this);
m_poll_hotkey_timer->Start(1000 / 60, wxTIMER_CONTINUOUS); m_poll_hotkey_timer.Start(1000 / 60, wxTIMER_CONTINUOUS);
} }
// Destructor // Destructor
CFrame::~CFrame() CFrame::~CFrame()
{ {
m_poll_hotkey_timer->Stop();
if (m_bHotkeysInit) if (m_bHotkeysInit)
{ {
Wiimote::Shutdown(); Wiimote::Shutdown();

View file

@ -18,6 +18,7 @@
#include <wx/mstream.h> #include <wx/mstream.h>
#include <wx/panel.h> #include <wx/panel.h>
#include <wx/string.h> #include <wx/string.h>
#include <wx/timer.h>
#include <wx/toplevel.h> #include <wx/toplevel.h>
#include <wx/windowid.h> #include <wx/windowid.h>
@ -47,8 +48,6 @@ class wxAuiNotebook;
class wxAuiNotebookEvent; class wxAuiNotebookEvent;
class wxListEvent; class wxListEvent;
class wxMenuItem; class wxMenuItem;
class wxTimer;
class wxTimerEvent;
class wxWindow; class wxWindow;
class CRenderFrame : public wxFrame class CRenderFrame : public wxFrame
@ -198,7 +197,7 @@ private:
EToolbar_Max EToolbar_Max
}; };
wxTimer* m_poll_hotkey_timer; wxTimer m_poll_hotkey_timer;
wxBitmap m_Bitmaps[EToolbar_Max]; wxBitmap m_Bitmaps[EToolbar_Max];
wxBitmap m_BitmapsMenu[EToolbar_Max]; wxBitmap m_BitmapsMenu[EToolbar_Max];

View file

@ -45,7 +45,7 @@ CLogWindow::CLogWindow(CFrame *parent, wxWindowID id, const wxPoint& pos,
const wxSize& size, long style, const wxString& name) const wxSize& size, long style, const wxString& name)
: wxPanel(parent, id, pos, size, style, name) : wxPanel(parent, id, pos, size, style, name)
, x(0), y(0), winpos(0) , x(0), y(0), winpos(0)
, Parent(parent), m_ignoreLogTimer(false), m_LogAccess(true) , Parent(parent), m_LogAccess(true)
, m_Log(nullptr), m_cmdline(nullptr), m_FontChoice(nullptr) , m_Log(nullptr), m_cmdline(nullptr), m_FontChoice(nullptr)
{ {
Bind(wxEVT_CLOSE_WINDOW, &CLogWindow::OnClose, this); Bind(wxEVT_CLOSE_WINDOW, &CLogWindow::OnClose, this);
@ -55,8 +55,8 @@ CLogWindow::CLogWindow(CFrame *parent, wxWindowID id, const wxPoint& pos,
CreateGUIControls(); CreateGUIControls();
m_LogTimer = new wxTimer(this); m_LogTimer.SetOwner(this);
m_LogTimer->Start(UPDATETIME); m_LogTimer.Start(UPDATETIME);
} }
void CLogWindow::CreateGUIControls() void CLogWindow::CreateGUIControls()
@ -177,8 +177,6 @@ CLogWindow::~CLogWindow()
{ {
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, this); m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, this);
} }
m_LogTimer->Stop();
delete m_LogTimer;
} }
void CLogWindow::OnClose(wxCloseEvent& event) void CLogWindow::OnClose(wxCloseEvent& event)
@ -287,7 +285,7 @@ void CLogWindow::OnWrapLineCheck(wxCommandEvent& event)
void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event)) void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event))
{ {
if (!m_LogAccess || m_ignoreLogTimer) if (!m_LogAccess)
return; return;
UpdateLog(); UpdateLog();
@ -302,13 +300,10 @@ void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event))
void CLogWindow::UpdateLog() void CLogWindow::UpdateLog()
{ {
if (!m_LogAccess || !m_Log) if (!m_LogAccess || !m_Log || msgQueue.empty())
return; return;
// m_LogTimer->Stop(); m_LogTimer.Stop();
// instead of stopping the timer, let's simply ignore its calls during UpdateLog,
// because repeatedly stopping and starting a timer churns memory (and potentially leaks it).
m_ignoreLogTimer = true;
std::lock_guard<std::mutex> lk(m_LogSection); std::lock_guard<std::mutex> lk(m_LogSection);
while (!msgQueue.empty()) while (!msgQueue.empty())
@ -351,7 +346,7 @@ void CLogWindow::UpdateLog()
msgQueue.pop(); msgQueue.pop();
} }
m_ignoreLogTimer = false; m_LogTimer.Start();
} }
void CLogWindow::Log(LogTypes::LOG_LEVELS level, const char *text) void CLogWindow::Log(LogTypes::LOG_LEVELS level, const char *text)

View file

@ -14,6 +14,7 @@
#include <wx/gdicmn.h> #include <wx/gdicmn.h>
#include <wx/panel.h> #include <wx/panel.h>
#include <wx/string.h> #include <wx/string.h>
#include <wx/timer.h>
#include <wx/translation.h> #include <wx/translation.h>
#include <wx/windowid.h> #include <wx/windowid.h>
@ -25,8 +26,6 @@ class wxBoxSizer;
class wxCheckBox; class wxCheckBox;
class wxChoice; class wxChoice;
class wxTextCtrl; class wxTextCtrl;
class wxTimer;
class wxTimerEvent;
// Uses multiple inheritance - only sane because LogListener is a pure virtual interface. // Uses multiple inheritance - only sane because LogListener is a pure virtual interface.
class CLogWindow : public wxPanel, LogListener class CLogWindow : public wxPanel, LogListener
@ -50,8 +49,7 @@ private:
CFrame* Parent; CFrame* Parent;
wxFont DefaultFont, MonoSpaceFont; wxFont DefaultFont, MonoSpaceFont;
std::vector<wxFont> LogFont; std::vector<wxFont> LogFont;
wxTimer* m_LogTimer; wxTimer m_LogTimer;
bool m_ignoreLogTimer;
LogManager* m_LogManager; LogManager* m_LogManager;
std::queue<std::pair<u8, wxString> > msgQueue; std::queue<std::pair<u8, wxString> > msgQueue;
bool m_writeFile, m_writeWindow, m_writeDebugger, m_LogAccess; bool m_writeFile, m_writeWindow, m_writeDebugger, m_LogAccess;