wxTimer causing memory leaks

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

wxTimer causing memory leaks

Post by deepti » Mon Nov 18, 2019 7:58 am

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.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2170
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer causing memory leaks

Post by PB » Mon Nov 18, 2019 8:43 am

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?

Kvaz1r
I live to help wx-kind
I live to help wx-kind
Posts: 162
Joined: Tue Jun 07, 2016 1:07 pm

Re: wxTimer causing memory leaks

Post by Kvaz1r » Mon Nov 18, 2019 8:50 am

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.

deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: wxTimer causing memory leaks

Post by deepti » Mon Nov 18, 2019 11:15 am

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!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2170
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer causing memory leaks

Post by PB » Mon Nov 18, 2019 11:30 am

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.

deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: wxTimer causing memory leaks

Post by deepti » Mon Nov 18, 2019 11:55 am

@PB, how about wxButton, wxStaticText, wxTextCtrl etc, created using new? They need to be deleted too ?

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2170
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxTimer causing memory leaks

Post by PB » Mon Nov 18, 2019 12:19 pm

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.

deepti
Earned some good credits
Earned some good credits
Posts: 115
Joined: Tue Jul 17, 2018 5:38 pm

Re: wxTimer causing memory leaks

Post by deepti » Mon Nov 18, 2019 12:23 pm

Got it. thanks a lot PB

alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 317
Joined: Tue Oct 18, 2016 2:31 pm

Re: wxTimer causing memory leaks

Post by alys666 » Mon Nov 18, 2019 8:22 pm

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.
ubuntu 16.04, wxWidgets 3.0.4

Post Reply