You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac调试C++代码时LLDB出现严重内存泄漏问题

调试C++代码时lldb-mi内存占用过高且VS Code卡顿的问题

问题现象

运行以下C++代码并调试时,在第11行vector<pair<int, int>> V;处设置断点会导致VS Code卡顿,16GB内存的MacBook Pro M4出现内存占用过高情况,经查是lldb-mi进程占用了30GB内存;但跳过该行设置断点,程序可正常运行。

代码示例

#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);

}

原因分析

这个问题大概率是lldb-mi在Apple Silicon平台上对嵌套结构的STL容器调试可视化存在兼容性问题。当断点停在vector<pair<int, int>> V;行时,调试器尝试解析容器的内部初始化结构,部分版本的lldb-mi处理这类嵌套容器时可能陷入内存泄漏或无限循环,导致内存暴增和VS Code卡顿。

解决办法

1. 禁用STL容器自动可视化

在VS Code的launch.json调试配置中添加设置,禁止调试器自动解析容器内容:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: clang++ 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "setupCommands": [
                {
                    "description": "启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "text": "settings set target.max-children-count 0"
                }
            ]
        }
    ]
}

2. 更新相关工具

  • 将VS Code的C/C++扩展更新到最新版本,微软会持续修复Apple Silicon平台的调试兼容性问题。
  • 重新安装Command Line Tools:终端运行xcode-select --install,确保lldb工具为最新版本。

3. 调整断点位置

暂时避开在STL容器声明行设置断点,将断点移到容器声明后的代码行(比如第12行map<char, int> tMap;之后),绕过调试器对容器初始化阶段的解析。

4. 临时修改代码

给容器添加显式初始化,减少调试器的解析压力:

vector<pair<int, int>> V{};

内容的提问来源于stack exchange,提问作者Bob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 21:43:12