计划退出未被召唤

Destructor not being called upon program exit

本文关键字:召唤 退出 计划      更新时间:2023-10-16

我正在编写一个非常简单的调试器,我定义了一个称为BREAKPOINT_INFOclass,其中包含有关设置断点的信息。

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);
            }
        }
    }
};

我需要驱动器来清理任何断点设置,但是解构器从未被调用,我不太确定为什么在我的特殊情况下这是。

这是main.cpp:

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

这是一个永无止境的循环,因此该程序目前从未真正达到返回。必须用Ctrl+C或通过关闭终端终止它。不确定这是否会导致灾难不被调用。

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

您必须处理sigint信号,否则程序将异常终止,并且未调用DTOR。