C++ gdb breakpooint in second thread causing core dump

Viewed 116

I have a C++ program I'm trying to debug in a new docker instance that is causing the program to crash when I try to hit a breakpoint in a created thread (but not the launch thread). Another version of the program run on a separate computer (different g++, gdb versions) works. I've created a test app to replicate the problem.

I'm using a ubuntu docker image (which is new to me). I've started a docker container with:

docker run -it -v "/home/test/":"/home/test" -w "/home/test" ubuntu

I've attached to this container in VS Code, in the /home/test/ folder. I've then installed g++ and gdb with: apt-get update then apt-get install g++ gdb. This installs versions g++: 4:11.2.0-1ubuntu1 gdb: 12.0.90-0ubuntu1

I then create a main.cpp with the following code:

#include <iostream>
#include <chrono>
#include <thread>

void thread_runner()
{
    while (1) {
        std::cout << "Background Thread" << std::endl;                //Second Breakpoint
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}

int main()
{
    std::cout << "Test App" << std::endl;                              // First Breakpoint
    std::thread ioc_thread = std::thread(thread_runner);                    // Create a separate (background) thread to run the io_context on
    ioc_thread.join();
}

And I set breakpoints on the 2 std::cout ... lines. I create a tasks.json file to compile this, which looks like:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "Task generated by Debugger."
        },
    ],
    "version": "2.0.0"
}

Then I run the command "Run and Debug" and use that task to compile. I hit the first breakpoint at "Test App" okay but it crashes before hitting "Background Thread", the terminal output is:

Test App
Aborted (core dumped)
[1] + Aborted (core dumped)      "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-mv2ltsxy.3ok" 1>"/tmp/Microsoft-MIEngine-Out-0zvvty3q.t43"

If I remove the second breakpoint the app runs printing "Background Thread" every second.

Apologies for the long winded description. I'm not sure where I'm going wrong so I looked to include everything. The other computer that appears to work (using my full original program) using g++ 9.3 and gdb 8.1.

0 Answers
Related