简体   繁体   English

GetMessage()破坏了我的堆栈?

[英]GetMessage() corrupting my stack?

I have had the following function used in a variety of different programs for many years. 多年来,我已经在各种不同的程序中使用以下功能。 It has always worked without error. 它一直没有错误地工作。 That is until now, when for the first time I attempted to make it work targeting 64bit code. 直到现在,我第一次尝试使它针对64位代码进行工作。

I try stepping through the code line by line... first of all I execute the line something_done = TRUE; 我尝试一行一行地执行代码...首先,我执行一行something_done = TRUE; which (obviously) sets something_done to 1... which I monitor in the "watch" window. 哪个(显然)将something_done设置为1 ...我在“监视”窗口中监视。 Then I execute GetMessage (&msg, NULL, 0, 0); 然后我执行GetMessage (&msg, NULL, 0, 0); ... but somehow during its execution, the variable something_done gets set to zero! ......但在执行过程中不知何故,可变something_done被设置成零! Subsequently the code then breaks out of the loop and , upon returning from the function call the compiler reports "Run-Time Check Failure #2 - Stack around the variable 'msg' was corrupted." 随后,代码随后跳出循环,从函数调用返回后,编译器报告“运行时检查失败#2-变量'msg'周围的堆栈已损坏”。

I'm not sure where to go from here. 我不确定从这里去哪里。

void process_messages_until_idle()
{
    MSG msg;
    int something_done;
    int temp;

    for (;;)
    {
        something_done = FALSE;

        temp = PeekMessage(&msg,NULL,0,0,PM_NOREMOVE);

        if (temp)
        {
            something_done = TRUE;
            GetMessage (&msg, NULL, 0, 0);
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }

        if (!something_done)
        {
            break;
        }
    }
}

UPDATE: I changed something_done into a static and now the program crashes somewhere else entirely - in a place that make more sense, so this will probably be easier to debug. 更新:我将something_done更改为static ,现在程序在其他地方完全崩溃-在更有意义的位置,因此这可能更易于调试。

Although there was a message when you did PeekMessage, something may have changed. 尽管在执行PeekMessage时出现了一条消息,但可能有所更改。

if (GetMessage(&msg, NULL, 0, 0) > 0) {
        something_done = TRUE;
        TranslateMessage (&msg);
        DispatchMessage (&msg);
}

I had this error on an x64 conversion of a project. 在项目的x64转换上出现此错误。 Turned out to be that we had /Zp1 struct member alignment on our project and this was changing the size of the MSG struct from 48 bytes to 40 which then caused stack corruption (and likely other unrealized issues) on x64. 原来是我们在项目上具有/ Zp1结构成员对齐,这将MSG结构的大小从48个字节更改为40个字节,然后导致x64上的堆栈损坏(以及其他可能未实现的问题)。 Under Win32 it worked fine. 在Win32下,它工作正常。

You are corrupting your stack somewhere. 您正在某个地方破坏堆栈。 Check which message you were processing at the time the stack was corrupted and inspect its code to see how it breaks. 检查堆栈损坏时您正在处理的消息,并检查其代码以了解其中断方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM