Page 1 of 1

wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 7:58 am
by deepti
Hi All,

Our application uses wxTimer instance like this.
Created like in step 1 below, started like in step 2, and stopped as in step 3.

Code: Select all

1. m_savedChangesTextHideTimer = new wxTimer(this);

2. m_savedChangesTextHideTimer->StartOnce(TEN_SECONDS);

3.
if (m_savedChangesTextHideTimer->IsRunning()
{
	m_savedChangesTextHideTimer->Stop();
}

Does anything else needs to be done?
Because when i attach a memory leak detector to the application, it shows leaks on the line where it is created.
Any help is very much appreciated.

Re: wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 8:43 am
by PB
And in which step do you delete the timer?

Also, is step 1 called more than once, which would mean multiple leaks?

Is there a reason why you create the timer with new?

Re: wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 8:50 am
by Kvaz1r
You should free memory. There is example how to do it correctly in gauge sample and you can use wxTimer not as pointer, example here.

Re: wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 11:15 am
by deepti
Thank for for your replies @PB and @Kvaz1r.
I have not deleted the timer at all. Ofcourse there is no need to create it with a new. It can be a static instance too.
But that is how ALL other wxWidgets objects in our application are created - using new, so did that with the timer as well.
We don't delete any other wxWidget instance created using new. Any reason why it needs to be done with wxTimer?

Thanks again!

Re: wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 11:30 am
by PB
deepti wrote: Mon Nov 18, 2019 11:15 am We don't delete any other wxWidget instance created using new. Any reason why it needs to be done with wxTimer?
The only wxWidgets new()ed variables that are not to be deleted (manually) are wxWindows and wxSizer(Item)s (except modal dialogs, but these should be created on the stack anyway). Anything else (e.g., wxString, wxTimer, wxColour, wxFFile, wxDateTime, wxVariant, ) must be somehow (manually, using a smart pointer) deleted.

Re: wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 11:55 am
by deepti
@PB, how about wxButton, wxStaticText, wxTextCtrl etc, created using new? They need to be deleted too ?

Re: wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 12:19 pm
by PB
deepti wrote: Mon Nov 18, 2019 11:55 am @PB, how about wxButton, wxStaticText, wxTextCtrl etc, created using new? They need to be deleted too ?
These are wxWindows, so generally no.

Re: wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 12:23 pm
by deepti
Got it. thanks a lot PB

Re: wxTimer causing memory leaks

Posted: Mon Nov 18, 2019 8:22 pm
by alys666
deepti wrote: Mon Nov 18, 2019 11:55 am @PB, how about wxButton, wxStaticText, wxTextCtrl etc, created using new? They need to be deleted too ?
all this controls have "parent" in their constructor parameters list. at destruction, the parent recursively deletes all childs(where childs delete their childs).
so if you provided a parent to such a window - parent will delete them at his deletion.
but if you provided a null parent - then you must delete it manually.