'Throwing exception in copying a set as the output of a recursive function to another set in C++

Consider the following function in C++:

 set<int> g2(int l){
    cerr<<"g2 "<<l<<" started"<<' '<<endl;
    set<int> jav;
    jav.clear();

    if(l==greed){
        cerr<<"g2 "<<l<<" ended"<<endl;
        return jav;
    }
    if(l==0){
        cerr<<"g2 "<<l<<" ended"<<endl;
        return jav;
    }
     if(sg2[l].size()){
        cerr<<"g2 "<<l<<" ended"<<endl;
        return sg2[l];
    }
   // cerr<<"alan injam"<<endl;
    set<int> y=g2(l-1);
    cerr<<"injam"<<endl;
    jav.insert(y.begin(),y.end());
    jav.insert(s[l].begin(),s[l].end());
    int t_l=tl(l);
    set<int> rr;
    rr.clear();
    rr=alt(l,t_l);
    jav.erase(rr.begin(),rr.end());
    sg2[l]=jav;
    cerr<<"g2 "<<l<<" ended"<<endl;
  //  cerr<<jav.size()<<endl;
    return jav;
}

Many times( but not always), the command set<int> y=g2(l-1), throws this exception:

Process returned -1073740940 (0xC0000374)

Do you know what is the reason and how can I solve it?

Please consider that operator = is defined for a set in C++ STL.

Here is the whole code:

https://github.com/SoroushVahidi/Scheduling-problem/blob/main/withset.cpp



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source