Bob
Bob

Reputation: 21

great memory leak on lldb in C++ code on mac

I ran a small C++ program:

#include <iostream>
#include <map>
#include <vector>
using namespace std;

class Solution
{
public:
    string minWindow(string s, string t)
    {
        vector<pair<int, int> > V;
        map<char, int> tMap;
        map<char, int> sMap;
        map<char, int> Map;
        int left = 0, right = 0;
        for (int i = 0; i < t.size(); i++)
        {
            tMap[t[i]]++;
        }

        cout << "tMap is " << endl;
        for (pair<char, int> temp : tMap)
        {
            cout << temp.first << "   " << temp.second << endl;
        }
        cout << endl;


        for (int i = 0; i < s.size(); i++)
        {
            sMap[s[i]]++;

            cout << "now the sMap is " << endl;
            for (pair<char, int> temp : sMap)
            {
                cout << temp.first << "   " << temp.second << endl;
            }
            cout << endl;

            bool include = true;
            for (pair<char, int> temp : tMap)
            {

                if (sMap[temp.first] < temp.second)
                {

                    include = false;
                    break;
                }
            }

            if (include)
            {
                cout << "find the answer" << endl;

                right = i;
                for (int j = i; j >= 0; j--)
                {
                    Map[s[j]]++;
                    bool find = true;
                    for (pair<char, int> temp : tMap)
                    {

                        if (Map[temp.first] < temp.second)
                        {

                            find = false;
                        }
                    }
                    if (find)
                    {
                        left = j;
                        V.push_back(pair<int, int>(left, right));
                        Map.clear();
                        break;
                    }
                }
            }
        }
        if(V.size()==0){
            return "";
        }
        cout<<endl;
        for(pair<int,int> temp : V){
            cout<<temp.first<<"   "<<temp.second<<endl;
        }
        cout<<endl;
        int answerLeft=V[0].first,answerRight=V[0].second,min=V[0].second-V[0].first;
        for (size_t i = 0; i < V.size(); i++)
        {
            if(min>V[i].second-V[i].first){
                answerLeft=V[i].first;
                answerRight=V[i].second;
                min=V[i].second-V[i].first;
            }
        }


        return s.substr(answerLeft,answerRight-answerLeft+1);
    }
};

int main()
{
    Solution *S=new Solution();
    string s = "ADOBECODEBANC", t = "ABC";
    cout << S->minWindow(s, t);

}

...and debugged it. Then I found that if I set a breakpoint in a certain line ,the vscode will be stuck, and if I skip the line to set a breakpoint then it will run normally. But suddenly, I found that my MacBookPro M4 16G 's Memory was under a great pressure, only to found a lldb-mi took up 30G memory.

and the stange line is line11 : vector<pair<int ,int> > V;

and the picture is below:

memory leak

vscode got stuck

Upvotes: 1

Views: 115

Answers (0)

Related Questions