Search code examples
c++wxwidgets

wxWidgets program increase CPU usage


I'm working on a wxWidgets program which is very similar to the wxWiki "Making a render loop" example using idle event. I modified the display panel to show a background image, a blinking text and an animation.

void MyApp::onIdle(wxIdleEvent& evt)
{
    if(render_loop_on)
    {
        drawPane->paintNow();

        while(Pending())
            Dispatch();
        evt.RequestMore();
    }
}

BasicDrawPane::BasicDrawPane(wxFrame* parent) :
wxPanel(parent)
{
    wxBitmap background;
    background.LoadFile("background.png", wxBITMAP_TYPE_PNG);
    m_pBackground = new wxStaticBitmap(this, -1, background);
    m_pBackground->Show();

    m_pText = new wxStaticText(this, -1, wxT("blinking"), wxPoint(400, 10));
    m_pText->Show();
    m_bShow = false;

    wxAnimation iconAni;
    iconAni.LoadFile("animation.gif");
    m_wxIconAnimationCtrl = new wxAnimationCtrl(this, -1, iconAni);
    m_wxIconAnimationCtrl->Play();
}

void BasicDrawPane::paintNow()
{
    m_bShow = !m_bShow;
    m_pText->Show(m_bShow);
}

I added the Dispatch function because without it the memory and CPU usage will increase linearly after program starts, but even with the Dispatch function the CPU usage will climb gradually after running for a few hours. CPU usage will be stable only when I display the background alone.

I did the test with wxWidgets 2.8.11 on puppy linux, can anyone advise it?


Solution

  • It is totally normal, that for this program the cpu usage increases. Look at the documentation of "RequestMore". This basically loops the idle-function so it is called repeatedly.

    Solution:

    Use a wxTimer for blinking the Text and remove the idle function.

    edit:

    Tipp of the Day (:D): Use wxWidgets 2.9.x, it contains a lot of bugfixes and improvements :).