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
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Memory Leaks

Post by Natulux »

Hey guys,

this maybe somewhat off topic, but I just don't understand, why I generate memory leaks.

I have a function, which is called every 500ms by a timer.

[EDIT:] The objects used are of one class calles "Patient".
The "patientsTemp" holds a local copy of the member array "m_patients"

Code: Select all

WX_DECLARE_OBJARRAY(Patient,      PatientenArray);
Patient *m_p;
PatientenArray m_patients;
PatientenArray patientsTemp = m_patients;
If the function uses the stack, it doesn't generate memory leaks (naturally):

Code: Select all

Patient patTemp("", "", "", "", "", "", "", "", "", "", "", "");
//some other stuff

patTemp.ms_dataDate = wxDateTime::Now().FormatISOCombined();
patTemp.ms_pid = pid;
patTemp.ms_firstname = firstname;
patTemp.ms_lastname = lastname;
patTemp.ms_birthday = birthday;
for(int z=0; z<patientsTemp.Count();z++)
{
	if(patientsTemp.Item(z).ms_pid == pid)
	{
		patientsTemp.RemoveAt(z);
		break;
	}
}
					
if(!bNewPatFound)
{
	*m_p = patTemp;
	bNewPatFound = true;
}
The same function using the heap instead is generating memory leaks:

Code: Select all

Patient *patTemp = new Patient("", "", "", "", "", "", "", "", "", "", "", "");
//some other stuff

patTemp->ms_dataDate = wxDateTime::Now().FormatISOCombined();
patTemp->ms_pid = pid;
patTemp->ms_firstname = firstname;
patTemp->ms_lastname = lastname;
patTemp->ms_birthday = birthday;
for(int z=0; z<patientsTemp.Count();z++)
{
	if(patientsTemp.Item(z).ms_pid == pid)
	{
		patientsTemp.RemoveAt(z);
		break;
	}
}
					
if(!bNewPatFound)
{
	*m_p = *patTemp;
	bNewPatFound = true;
}

delete patTemp;
//wxDELETE(patTemp); //isn't working either
Since I delete my object at the end, I don't quite understand the leak. Is it maybe because of the copying to the member variable?

Code: Select all

*m_p = *patTemp;
[EDIT:] In addition to the code above:

Code: Select all

m_patients.Add(m_p);
[EDIT:]
And relatet to this: Does this generate mem leaks?

Code: Select all

wxExecute(exec, wxEXEC_ASYNC, new wxProcess(wxPROCESS_DEFAULT));
In my understanding, it should but it seems like it doesn't (it is not my code and I wonder, how much I should change).
But by changing this, I even crashed my application:

Code: Select all

wxProcess execProcess(wxPROCESS_DEFAULT);
long lExec = wxExecute(exec, wxEXEC_ASYNC, &execProcess);
Why? Because of "ASYNC" and my wxProcess object is already destroyed when it finishes?

Greets
Natu
Last edited by Natulux on Thu Sep 07, 2017 7:38 am, edited 7 times in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Memory Leaks

Post by doublemax »

The deleting of "patTemp" seems ok to me, but what about "patientsTemp"? I would suspect the leak there.

Regarding wxProcess read the note near the beginning in the docs:
http://docs.wxwidgets.org/trunk/classwx_process.html
Use the source, Luke!
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Memory Leaks

Post by Natulux »

doublemax wrote:The deleting of "patTemp" seems ok to me, but what about "patientsTemp"? I would suspect the leak there.
It is a local copy from a class array

Code: Select all

//from:
//WX_DECLARE_OBJARRAY(Patient,      PatientenArray);
PatientenArray patientsTemp = m_patients;
Since it is created on the stack, it is deleted automatically when the function ends. And please note, that I tested both functions and have no memory leaks, when I use the function with the array on the stack. So it has got to do something with this object heap/stack initialisation/deletion process.
doublemax wrote:Regarding wxProcess read the note near the beginning in the docs:
http://docs.wxwidgets.org/trunk/classwx_process.html
Ahh yea, I even read that before. Thank you ;-)

Greets Natu
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Memory Leaks

Post by doublemax »

In the "leak" version you posted, "patientsTemp" is also a pointer:

Code: Select all

for(int z=0; z<patientsTemp->Count();z++)
So it's unclear (to me) whether it's allocated on the heap or the stack.
Use the source, Luke!
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Memory Leaks

Post by evstevemd »

Natulux wrote:Hey guys,
...........
And relatet to this: Does this generate mem leaks?

Code: Select all

wxExecute(exec, wxEXEC_ASYNC, new wxProcess(wxPROCESS_DEFAULT));
Greets
Natu
No it should not. The docs say if you aren't catching the terminate event allocate wxProcess on heap as it will "self-delete".

I think DM have great point. Show how all pointer in your code above are allocated (in words or code) and it will make easier for someone to provide something more than intelligent guess!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Memory Leaks

Post by Natulux »

doublemax wrote:In the "leak" version you posted, "patientsTemp" is also a pointer:

Code: Select all

for(int z=0; z<patientsTemp->Count();z++)
So it's unclear (to me) whether it's allocated on the heap or the stack.
Im sorry, I edited the code a little to post it here and I made a mistake - I corrected my post above. This array is always allocated on the stack.
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Memory Leaks

Post by Natulux »

evstevemd wrote:I think DM have great point. Show how all pointer in your code above are allocated (in words or code) and it will make easier for someone to provide something more than intelligent guess!
Yes, I made a "search and replace" mistake for the code to post here. Sorry.
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Memory Leaks

Post by doublemax »

In that case i really can't see where the leak could come from.

What happens if you remove the line "*m_p = *patTemp;" ?

With which tool do you detect the memory leak? If you're using VS under Windows, please try
Visual Leak Detector, it should tell you exactly where the leak is.
https://vld.codeplex.com/
Use the source, Luke!
Natulux
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 242
Joined: Thu Aug 03, 2017 12:20 pm

Re: Memory Leaks

Post by Natulux »

doublemax wrote:What happens if you remove the line "*m_p = *patTemp;" ?

With which tool do you detect the memory leak? If you're using VS under Windows, please try
Visual Leak Detector, it should tell you exactly where the leak is.
https://vld.codeplex.com/
Surprisingly, I still have memory leaks with those lines commented out.

I don't really use a tool to detect the mem leaks, I just had the windows 10 task manager opened and watched my process increase in size until it crashes.
And yes, I use VS (2008 express atm). I'll try that detector, thank you. :-)

[EDIT:] In order to run vld, I need to compile in debug mode as it seems?
My debug mode is broken atm, the compiler complains about:
Incompatability between "P1" version "20080116" and "P2" version "20070207"
User avatar
doublemax
Moderator
Moderator
Posts: 19117
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Memory Leaks

Post by doublemax »

Code: Select all

Incompatability between "P1" version "20080116" and "P2" version "20070207"
One of the libraries was built with a different compiler (version). Try a clean rebuild of both the wxWidgets libraries and your project (and any other 3rd party library you may link to).
Use the source, Luke!
Post Reply