control-c 후 Visual Studio 콘솔 프로그램 충돌 (visual studio console program crashes after control-c)


문제 설명

control‑c 후 Visual Studio 콘솔 프로그램 충돌 (visual studio console program crashes after control‑c)

Visual Studio 2019의 최신 업데이트(버전 16.5.0 Preview 2.0)를 사용 중이며 control‑c 이벤트를 가로채려고 합니다. 그러나 어떤 이유로 콘솔 프로그램은 항상 내 핸들러를 호출하기 전에 충돌합니다. 그런데 핸들러를 설치하지 않아도 프로그램이 다운됩니다.

컴파일러의 버그일까요? 일반적으로 이와 같은 것을 어떻게 디버깅합니까?

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <cstdint>

uint64_t iterations;
bool running;

BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
    switch (fdwCtrlType) {
        case CTRL_C_EVENT: {
            running = false;
            return TRUE;
        } break;
        default: {
            return FALSE;
        } break;
    }
}

int main()
{
    if (!SetConsoleCtrlHandler(CtrlHandler, TRUE)) {
        std::cout << "Could not install control handler" << std::endl;
    }
    running = true;
    while (running) {
        iterations++;
    }
    std::cout << "Terminated after " << iterations << " iterations." << std::endl;
    return 0;
}

여기에 이미지 설명 입력


참조 솔루션

방법 1:

https://docs.microsoft.com/en‑us/windows/console/setconsolectrlhandler

Here it says that if an application is being debugged, "the system generates a DBG_CONTROL_C exception. This exception is raised only for the benefit of the debugger". Hence it isn't chrashing; you can continue excectution and it should work fine. The documentation says:

If the debugger passes the exception on unhandled, CTRL+C is passed to the console process and treated as a signal, as previously discussed.

(by NowibananatzkiLukas Thiersch)

참조 문서

  1. visual studio console program crashes after control‑c (CC BY‑SA 2.5/3.0/4.0)

#C++ #Windows #visual-studio






관련 질문

파일의 암호화/복호화? (Encryption/ Decryption of a file?)

이상한 범위 확인 연산자가 있는 C++ typedef (C++ typedef with strange scope resolution operator)

개체 배열 매개변수--오류: '문자' 필드에 불완전한 유형이 있습니다. (Object array parameter--error: field ‘letters’ has incomplete type)

C++에서 메모리 삭제 (Deleting memory in c++)

C++ 프로그램을 실행할 수 없습니다. No se ejecuta el programa (C++ i can't run a program. No se ejecuta el programa)

컴파일 시 변수의 이름과 수명 (Name and lifetime of variables at compile time)

control-c 후 Visual Studio 콘솔 프로그램 충돌 (visual studio console program crashes after control-c)

멤버 변수에 std::enable_if 또는 유사한 메서드 사용 (Using std::enable_if or similar method on member variable)

ifstream input_file(filename); (I am receiving an error "no matching function to call" in the line ifstream input_file(filename);)

ESP8266에서 잠시 실행하면 JsonData 크기가 0이 됩니다. (JsonData size becomes zero after awhile of running in ESP8266)

합에 대한 속도 문제(제수의 합) (Speed problem for summation (sum of divisors))

벡터 벡터에서 하위 벡터의 첫 번째 값을 찾기 위해 begin() 및 end() 반복기의 범위를 지정하는 방법은 무엇입니까? (How to specify a range for begin() and end() iterators to find first value of sub vector in a vector of vectors?)







코멘트