程序退出时不会调用析构函数

时间:2017-04-29 18:31:28

标签: c++ debugging winapi destructor

我正在编写一个非常简单的调试器,我定义了一个名为class的{​​{1}},其中包含有关断点集的信息。

BREAKPOINT_INFO

我需要析构函数来清理任何断点集,但解构器永远不会被调用,而且我不太确定为什么会出现这种情况。

这里是main.cpp:

class BREAKPOINT_INFO
{
public:

    HANDLE hProcess;

    PCHAR  lpBreakPoint;

    CHAR   instr;

    BOOL   justCalled;

    //Set default values
    BREAKPOINT_INFO()
    {
        hProcess     = NULL;

        lpBreakPoint = NULL;

        instr        = 0x00;

        justCalled   = FALSE;
    }

    //Destructor
    ~BREAKPOINT_INFO()
    {
        //Let me know the destructor is being called
        MessageBox(NULL, "Destructor called", NULL, MB_OK);

        DWORD dwError        = 0;
        LPCSTR szErrorRest   = (LPCSTR)"Error restoring original instruction: ";
        LPCSTR szErrorHanlde = (LPCSTR)"Error closing process handle: ";
        std::ostringstream oss;

        if(hProcess != NULL && lpBreakPoint != NULL)
        {
            //write back the original instruction stored in instr
            if(!WriteProcessMemory(hProcess, lpBreakPoint, &instr, sizeof(CHAR), NULL))
            {
                dwError = GetLastError();

                oss << szErrorRest << dwError;

                MessageBox(NULL, oss.str().c_str(), "ERROR", MB_OK|MB_ICONERROR);
            }
        }
    }
};

这是一个永无止境的循环,所以此刻的程序实际上永远不会达到回报。它必须以BREAKPOINT_INFO instrMov; //GetProcModuleHandle is a function I made to get the handle of a //of a module in a remote process LPVOID lpServerDll = (LPVOID)GetProcModuleHandle(dwPid, szServerDll); //the instructions address is relative to the starting address of the server dll. Hence the offset. PCHAR lpInstr = (PCHAR)((DWORD)lpServerDll+instr_offset); hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwPid); //sets the breakpoint instrMov.InitializeBreakPoint(hProcess, lpInstr); while(1) { if(!instrMov.justCalled) { instrMov.SetBreakPoint(); } if(instrMov.justCalled) { instrMov.justCalled = FALSE; } if(WaitForDebugEvent(&dbgEvent, 0)) { ProcessDebugEvent(&dbgEvent, lpBreakPoints, 3); ContinueDebugEvent(dbgEvent.dwProcessId, dbgEvent.dwThreadId, DBG_CONTINUE); } } return 0; //<---never reaches return 或关闭终端终止。不确定这是否会导致析构函数被调用。

任何信息,解决方案等都将不胜感激。谢谢你的时间。

1 个答案:

答案 0 :(得分:-3)

你必须处理SIGINT信号,否则程序会异常终止并且不会调用dtors。