7

I have problem in using Visual Studio Code to debug c++11 code on Mac.

Specifically, my codes can be compiled (using g++/clang++) and run without problem. But when I use debug mode in VS code, run my code step by step, it will stuck at some random point. Then it has no reaction when click on step into/step over. The Variable Window on the left side will show a loading circle forever, see below. Also my laptop becomes hot and the fan will run at this time.

enter image description here

I have tried the topological sort code below. Sometimes it stucks at the 1st line right after main function, while sometimes it stucks at somewhere else. Without running step by step, the code can be executed till the end and get correct result.

I have no idea what's going on, so I reinstall it but the problem exists. I attached the task.json config files along with the code I used for testing. Please help me and give suggestions. Thanks in advance!

Below is my tasks.json used to set debug configuration:

"version": "2.0.0",
"tasks": [
  {
    "type": "shell",
    "label": "clang++ build active file",
    "command": "/usr/bin/clang++",
    "args": [
      "-std=c++11",
      "-stdlib=libc++",
      "-g",
      "${file}",
      "-o",
      "${fileDirname}/${fileBasenameNoExtension}"
    ],
    "options": {
      "cwd": "${workspaceFolder}"
    },
    "problemMatcher": ["$gcc"],
    "group": {
      "kind": "build",
      "isDefault": true

Below is my code for topological sort in c++11:

#include <queue>
#include <unordered_map>
#include <vector>
#include <iostream>

using namespace std;

class Solution {
  public:
    vector<int> topoSort(vector<vector<int>>& edgeInfo) {
    queue<int> zeroDegree;
    // ->(stuck at next line, without reaction when click on step into/over)
    unordered_map<int, int> inDegree;
    vector<int> answer;
    vector<vector<int>> graph(4);

    for (auto edge: edgeInfo) {
      graph[edge[0]].push_back(edge[1]);
      inDegree[edge[1]]++;
    }

    for (int i=0; i<graph.size(); ++i) {
      if (inDegree[i] == 0) {
        zeroDegree.push(i);
        answer.push_back(i);
      }
    }
 
    while (!zeroDegree.empty()) {
      int curr_vertex = zeroDegree.front();
      zeroDegree.pop();
      for (auto neighbor: graph[curr_vertex]) {
        inDegree[neighbor]--;
        if (inDegree[neighbor] == 0) {
          zeroDegree.push(neighbor);
          answer.push_back(neighbor);
        }
      }
    }
    return answer;
  }
};

int main() {
  // ->(or stuck at next line)
  vector<vector<int>> edgeInfo = {{0,1}, {1,2}, {1,3}};
  Solution topologicalSort;
  vector<int> answer = topologicalSort.topoSort(edgeInfo);

  for (auto node: answer) {
    cout << node << "-> ";
  }
  cout << "\n";
  return 0;
}
rioV8
  • 24,506
  • 3
  • 32
  • 49
zurich_ruby
  • 315
  • 1
  • 3
  • 8
  • What is the need for the class `Solution`, this is not Java or C#, `tasks,josn` is for compiling , `launch.json` is for debugging – rioV8 Oct 05 '21 at 07:54
  • @rioV8 hi thanks for reply. the use of class Solution might be optional, but I don't think it the issue that freeze and stuck Local in Variables window, when I debug. And yes I have both tasks.json and launch.json files. The debug function works for simple codes, but not for code with queue, vector, unordered_map data type. I don't know why. It just stuck there. Btw can my code debug successfully on your VS code? Thanks! – zurich_ruby Oct 05 '21 at 08:08
  • No problems in VSC-MinGW-Windows-C++17. Don't use `using namespace std;`, explanation can be found after search this site, don't hard code the size of `graph`, do not assume the node labels start at `0` – rioV8 Oct 05 '21 at 11:28

1 Answers1

1

I do not know what causes this, or how to fix the real problem, but I have found a workaround to this problem for me.

I am running macOS Monterey on a 2013 MacPro, also was having this issue in VScode for C++ development.

On a tip from a friend, I installed the CodeLLDB extension. Don't forget to add the "type": "lldb" property to your launch.json to integrate CodeLLDB.

This fixed the issue for me, on top of having a slightly cooler debugger to boot.