Dynamic Memory Delete Problem

I'm not so good at this so apologies for any obvious mistakes.
My problem consists of two functions of a wider algorithm. To simplify I will call them U and S. In a "for" loop, I use U conditionally (using if) and S two times each for each round (I will call them U1, U2, S1 and S2). U1 and U2 comes before S1 and S2. Both functions have variables created and deleted inside the function according to dynamic memory principles.

The problem occurs after some progress in the loop. During that period S1 and S2 work seamlessly and U1 and U2 don't work at all because conditions did not apply yet. Then, U1 and U2 kick in, still no problem, S1 kick in, still no problem they all pass, but S2 fails. The failure occurs at the deletion of an array. There are other arrays in the function and they do not fail at all.

The interesting point is I can delay this failure by simply introducing a dummy string function (see below). Though after the second time the U functions' occurence, my S2 fails again.

1
2
3
4
5
6

string ABC(){

return "a";
}


S1 and S2 are the same function working on different variables/arrays of the same type, as U1 and U2. U1 and S1 work on the same variables/arrays.

How can I overcome this obstacle? Any ideas?

I can provide the code snippets if you like, but I doubt it would help.
The interesting point is I can delay this failure by simply introducing a dummy string function (see below)


Strange behavior that doesn't make any sense = smells like heap corruption.

But really, it's kind of hard to follow what you're talking about.

Can you post the code for U and S?

Last edited on
The code I'm working on is a genetic algorithm (it may be useful info for those interested in GAs). Each "children" has a chromosome (children[]->chromosome[]) consisting of genes containing information (a number in this case).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

for(int memNo = 0; memNo < populationSize; memNo = memNo+2){

children[memNo] 	= new solution();
children[memNo+1] 	= new solution();
par->crossOver(parMate, children[memNo], children[memNo+1], randomSeed);


            children[memNo]->calculateObjectives();
            children[memNo+1]->calculateObjectives();

//Originally there is a condition here
            children[memNo]->uplift(mutationProbability, randomSeed);
            children[memNo+1]->uplift(mutationProbability, randomSeed);


            children[memNo]->smarterMutate(mutationProbability, randomSeed);
	    children[memNo+1]->smarterMutate(mutationProbability, randomSeed);
}


My S function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void solution::smarterMutate(double mutationProbability, int randomSeed){

	double *probMutate = new double(0.0);

    int *availables, *facilityUtil;
    facilityUtil = new int [numberOfFacilities];
    availables = new int [numberOfFacilities];
    int *count = new int(0);

    for(int j=0; j < numberOfFacilities; j++){
        facilityUtil[j] = 0;
        availables[j] = 0;
    }

    for(int j=0; j < numberOfDemands; j++){
        facilityUtil[chromosome[j]] += demandValues[j];
    }

    for(int j=0; j<numberOfFacilities; j++){
        if(facilityUtil[j] > 0){
            availables[*count] = j;
            (*count)++;
        }
    }

	for(int i = 0; i < numberOfDemands; i++){
        *probMutate = lcgrand(randomSeed);

        if(*probMutate < mutationProbability || chromosome[i] >= numberOfFacilities){
            *probMutate = availables[(int)(lcgrand(randomSeed) * (*count))];
            chromosome[i] = chromosome[(int) *probMutate];
        }

        if(chromosome[i] >= numberOfFacilities){chromosome[i] = 0;}
	}

    }

    delete probMutate;
    delete [] facilityUtil;

//Here is where things go crazy. I try to delete availables and it snaps
    delete [] availables;  

    delete count;

    return;
}



My U function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

void solution::uplift(double mutationProbability, int randomSeed){

//The victim is the facility to kick out of the chromosome
    int *victim = new int(numberOfFacilities);

//Gross gain will help the total potential benefit of a facility among the active facilities

    double *grossGain = new double[numberOfFacilities];

//Good old facility utilization counter

    bool *facilityActive = new bool [numberOfFacilities];

    for(int i = 0; i <numberOfFacilities; i++){
        facilityActive[i] = false;
        grossGain[i] = 0.0;
    }

//topDog is the variable that indicates the higher coverage value among the active facilities
    double *topDog = new double(0.0);

    for(int i = 0; i < numberOfDemands; i++){
        facilityActive[chromosome[i]] = true;
    }

    for(int i = 0; i < numberOfDemands; i++){
        *topDog = 0;
        for(int j = 0; j < numberOfFacilities; j++){
            if(coverageMatrix[j][i] > *topDog && facilityActive[j]){
                *topDog = coverageMatrix[j][i];
                if(*topDog == 1)
                    break;
            }
        }

        if(*topDog > 0){
            for(int j = 0; j < numberOfFacilities; j++){
                if(coverageMatrix[j][i] == *topDog){
                    grossGain[j] += demandValues[i]*coverageMatrix[j][i]*1.5;
                }
                else{
                    grossGain[j] += demandValues[i]*coverageMatrix[j][i];
                }
            }
        }
    }

    // Now topDog is used as the sum of grossGains in order to use in the selection process
    *topDog = 0;

    for(int i = 0; i < numberOfFacilities; i++){
        if(i >= numberOfExistingFacilities){
            grossGain[i] *= 0.9;
        }
        grossGain[i] = 1/grossGain[i];
        *topDog += grossGain[i];
    }

    *topDog = lcgrand(randomSeed)*(*topDog);

    for(int i = 0; i < numberOfFacilities; i++){
        if(*topDog < grossGain[i]){
            *victim = i;
            break;
        }
        else{
            grossGain[i+1] += grossGain[i];
        }
    }

    delete [] grossGain;
    delete [] facilityActive;

//    cout << "The facility to close is:" << *victim + 1 << endl;

    for(int i = 0; i <numberOfDemands; i++){
        if(chromosome[i] == *victim){
            chromosome[i] = numberOfFacilities;
        }
    }

    delete victim;
    delete topDog;

    return;
}



Happens again with another function. It seems like I cannot delete two arrays sequentially.
The interesting thing is by adding cout lines (e.g. cout << "Blah Blah Blah" << endl;) actually helps the code run a little longer.
Topic archived. No new replies allowed.