I was 100% positive that I covered all ground in terms of deleting memory from the heap before it was lost, but valgrind seems to disagree. Any help with finding the leak in the following code would be greatly appreciated! I can't seem to figure out what's causing it
Card * S = new Card[subsetSize];
Card * M = nullptr;
int subsetPrice = 0, subsetProfit = 0;
for(int i = 0; i < subsetSize; i++){
S[i] = problemCards[cardIndexesToAdd[i]];
subsetPrice += S[i].getPrice();
subsetProfit += S[i].getProfit();
}
// Evaluate the subset's cost and profit
if(subsetPrice <= maxToSpend){
if(subsetProfit > maxProfit){
maxProfit = subsetProfit;
if(M != nullptr)
delete[] M;
M = S;
S = nullptr;
mSize = subsetSize;
}
}
else{
if(S != nullptr){
delete[] S;
S = nullptr;
}
}
// output code for M
if(M != nullptr)
delete[] M;