Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode debugger freezes

I am trying to debug a C++ file in VSCode. The debugger works fine while it is in the main() function, but as soon as it enters a function, the debugger freezes and the control panel no longer works(except the stop button). In the left bar, it shows the loading icon next to "Local" variables, which loads perpetually.

I am on macOS 11.3.1, and VSCode Version: 1.58.2 (Universal).

Here are my files: debug.cpp

#include<iostream> 
#include<queue> 
#include<vector>
#include<map> 
#include<stack> 
#include<unordered_map>
#include<string>
using namespace std;

int longestConsecutive(vector<int>& nums) {

    unordered_map<int, bool> hash;
    for(int i = 0; i<nums.size(); i++){
        hash[nums[i]] = true;
    }

    int max_range = 0;
    for(int i = 1; i<nums.size()-1; i++){
        if(hash[nums[i]]){
            hash[nums[i]] = false;
            int j = i-1;
            int leftRange = 0;
            while(hash[nums[j]] && j>=0){
                hash[nums[j]] = false;
                leftRange++;
                j--;
            }
            int k = i+1;
            int rightRange = 0;
            while(hash[nums[k]] && k<nums.size()){
                hash[nums[k]] = false;
                rightRange++;
                k++;
            }
            if(leftRange+rightRange+1 > max_range){
                max_range = leftRange+rightRange+1;
            }
        }
    }
    return max_range;

}

int main(){
    vector<int> vec;
    vec.push_back(100);
    vec.push_back(4);
    vec.push_back(200);
    vec.push_back(1);
    vec.push_back(3);
    vec.push_back(2);

    int ans = longestConsecutive(vec);

    cout<<ans<<endl;
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "clang++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ build active file"
        }
    ]
}

A thing to note, when I change my longestConsecutive function in debug.cpp, and make it something entirely random like:

int temp;
temp = 100;
return temp;

then the debugger works fine. Is there something wrong with the code I wrote?

Thanks in advance!

like image 738
Aditya Prakash Avatar asked Sep 11 '25 18:09

Aditya Prakash


1 Answers

I 've met this situation exactly before. just change configurations [type] from "cppdbg" to "lldb" in launch.json solve this problem. hope it helps you.

like image 110
kenryhuang Avatar answered Sep 14 '25 08:09

kenryhuang