Skip to content

Commit 06d75a0

Browse files
author
Chris Sullivan
committed
Put temporary Genome child on the stack and fixed memory leak.
1 parent 1a3a2eb commit 06d75a0

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

libNeat/src/Population.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,24 @@ Population Population::Reproduce() {
136136
}
137137
Organism& parent1 = species[idx1];
138138
Organism& parent2 = species[idx2];
139-
Genome* child = new Genome;
139+
Genome child;
140140

141141
// determine relative fitness for mating
142142
if (parent1.fitness > parent2.fitness) {
143-
*child = parent1->MateWith(*parent2);
143+
child = parent1->MateWith(*parent2);
144144
} else if (parent2.fitness > parent1.fitness) {
145-
*child = parent2->MateWith(*parent1);
145+
child = parent2->MateWith(*parent1);
146146
} else {
147147
// break a fitness tie with a check on size
148148
if (parent1->Size() > parent2->Size()) {
149-
*child = parent1->MateWith(*parent2);
149+
child = parent1->MateWith(*parent2);
150150
}
151151
else { // equal size or parent 2 is larger
152-
*child = parent2->MateWith(*parent1);
152+
child = parent2->MateWith(*parent1);
153153
}
154154
}
155-
child->Mutate();
156-
progeny.push_back(*child);
155+
child.Mutate();
156+
progeny.push_back(child);
157157

158158

159159
}

0 commit comments

Comments
 (0)