Click to See Complete Forum and Search --> : Memory Leak


mathsprogrammer
August 7th, 2004, 06:08 AM
I have written a c++ program in visual studio. Inside the program I make reference to the System.Random() class in .NET. My program contains a very computationally intensive loop that loops 300000 times and inside the loop certain fuctions are performed over and over again.

My problem is that on running the program, it uses up to 250MB of memory. This is obviously a problem as it really needs to run for much longer (shorter runs use up much less memory).

I was thinking that my variables and arrays that I define in a function (one that is referred to 28 time inside the large loop - so runs 300000*28 times) are redefined every time the function runs and thus are taking up a different place in memory every time.

I do, however, define my arrays using pointers as follows:
int *consecutive;
consecutive = new int[Planning_Horizon_Length];

and then run
delete[] consecutive;
at the end of the function. Would I be right in saying that this is performing memory clean ups on it?

Obviously, any variables defined in these functions are defined directly as
int i, j;
Could this be where my problem is?

Lastly, I have a 3 dimensional global array (defined using pointers again) that these functions update. Is there a special way to update this array or will:
for (k1 = 0; k1 < Planning_Horizon_Length; k1++)
{
for (k2 = 0; k2 < Number_Of_Shifts; k2++)
{
X_temp[k1][k2] = X[i][k1][k2];
X_temp1[k1][k2] = X[j][k1][k2];
}
}
work fine?

Any help would be appreciated.

KevinHall
August 7th, 2004, 12:56 PM
It's difficult to analyze memory leaks from such a high-level description. If your program is small, you could post your code.

You should also look into using MSVC's runtime memory tracking functions.

Another idea still is to place breakpoints on your new's and your delete's and make sure that a delete is getting called for every new.

Andreas Masur
August 8th, 2004, 05:36 AM
[ Moved thread ]

Andreas Masur
August 8th, 2004, 05:44 AM
Well...due to the garbage collection of the .NET framework it is not possible to have a memory leak in managed code...are you calling unmanaged code parts as well?


Detecting and Isolating Memory Leaks (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxcondetectingisolatingmemoryleaks.asp)
Detecting Memory Leaks in MFC (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/_core_detecting_memory_leaks.asp)

mathsprogrammer
August 8th, 2004, 08:03 AM
void crossover(int i, int j)
{
int point1, point2, **X_temp, **X_temp1, k1, k2;

Random* randObj = new Random( );

// Assign memory
X_temp = new int*[Planning_Horizon_Length];
for (k1 = 0; k1 < Planning_Horizon_Length; k1++) X_temp[k1] = new int[Number_Of_Shifts];

X_temp1 = new int*[Planning_Horizon_Length];
for (k1 = 0; k1 < Planning_Horizon_Length; k1++) X_temp1[k1] = new int[Number_Of_Shifts];

//randomly choose two crossover points (where point1<point2)

point1=Math::Floor((randObj->NextDouble()) * Planning_Horizon_Length);
point2=Math::Floor((randObj->NextDouble()) * Planning_Horizon_Length);
while (point1 >= point2)
{
point1=Math::Floor((randObj->NextDouble()) * Planning_Horizon_Length);
point2=Math::Floor((randObj->NextDouble()) * Planning_Horizon_Length);
}

//perform crossover

for (k1 = 0; k1 < Planning_Horizon_Length; k1++)
{
for (k2 = 0; k2 < Number_Of_Shifts; k2++)
{
X_temp[k1][k2] = X[i][k1][k2]; // store midwife 1 in a temporary location
X_temp1[k1][k2] = X[j][k1][k2]; // store midwife 2 in a temporary location
}
}

for (k1 = 0; k1 < Planning_Horizon_Length; k1++)
{
for (k2 = 0; k2 < Number_Of_Shifts; k2++)
{
if ((k1 > point1) && (k1 < point2))
{
// copy contents of midwife 2 to midwife 1 between crossing points
X[i][k1][k2] = X[j][k1][k2];
// copy contents of temp to midwife 2 betwen crossing points
X[j][k1][k2] = X_temp[k1][k2];
}
}
}

// we only want to accept feasible crossovers

if (hard() == false)
{
for (k1 = 0; k1 < Planning_Horizon_Length; k1++)
{
for (k2 = 0; k2 < Number_Of_Shifts; k2++)
{
if ((k1 > point1) && (k1 < point2))
{
// copy contents of midwife 2 to midwife 1 between crossing points
X[i][k1][k2] = X_temp[k1][k2];
// copy contents of temp to midwife 2 betwen crossing points
X[j][k1][k2] = X_temp1[k1][k2];
}
}
}
}

// Free memory

for (k1 = 0; k1 < Number_Of_Shifts; k1++) delete[] X_temp[k1];
delete[] X_temp;
for (k1 = 0; k1 < Number_Of_Shifts; k1++) delete[] X_temp1[k1];
delete[] X_temp1;

return;
}

I have isolated the problem to the section of code above. Is there something I'm doing wrong in it obvious? The global variables used in it are:

int Planning_Horizon_Length = 28;
int Number_Of_Shifts = 4;

X = new int**[Number_Of_Nurses];
for (i = 0; i < Number_Of_Nurses; i++) X[i] = new int*[Planning_Horizon_Length];
for (i = 0; i < Number_Of_Nurses; i++)
for (j = 0; j < Planning_Horizon_Length; j++) X[i][j] = new int[Number_Of_Shifts];

Thanks for the help.

rsnic
August 9th, 2004, 09:14 AM
Maybe something like this:

for (k1 = 0; k1 < Planning_Horizon_Length; k1++) {
for(int i = 0; i < Number_Of_Shifts;i++){
delete (int *)X_temp[k1];
}
delete[] X_temp[k1];
}

rsnic
August 9th, 2004, 10:14 AM
Oh and put this after it

delete X_temp;

mathsprogrammer
August 11th, 2004, 06:24 AM
Found a stray delete where there should have been a delete[]. Seems to have fixed the problem.