getting heap use after free error in Leetcode, don't seem understand the root cause.Can you guys help me out here?
Mostly everything is declared on stack. My only suspect is the shallow copy I create on the stack multiset temp but you can't free anything on the stack right which is not created on heap?
class Solution {
public:
void earn_points(multiset<int> points,int currpoint, int& max){
set<int> unique;
if(points.size() == 0){
cout <<"finalscore="<< currpoint << " "<<endl;
if(currpoint > max){
max = currpoint;
}
}
multiset<int> temp = points;
for(auto it=points.begin(); it != points.end(); ++it){
int num = *it;
if(unique.find(num) != unique.end()){
continue;
}
unique.insert(num);
int delete_num1 = num + 1;
int delete_num2 = num - 1;
points.erase(it);
if(points.find(delete_num1) != points.end())
points.erase(delete_num1);
if(points.find(delete_num2) != points.end())
points.erase(delete_num2);
cout << num <<" ";
for(auto i : points){
cout << i <<" ";
}
cout << endl;
earn_points(points,currpoint + num,max);
points = temp;
}
}
int deleteAndEarn(vector<int>& nums) {
multiset<int> points(nums.begin(),nums.end());
int max = INT32_MIN;
earn_points(points,0,max);
return max;
}
};