开发者

C++ function exits arbitrarily

开发者 https://www.devze.com 2023-02-25 03:29 出处:网络
I have a weird problem: On my Win32 C++ App, I have a function where the function returns after a call to another function.

I have a weird problem:

On my Win32 C++ App, I have a function where the function returns after a call to another function.

void f()
{
    //SECTION 1//

    if( interactFrame )
    {
        psFrame->getWindow()->deactivate();
        interactFrame = activeFrame = 0;
        logFile << "PS deactive" <开发者_Go百科< endl;
    }        

    //SECTION 2//
}

void Window::deactivate()
{
    SetLayeredWindowAttributes( handle_, 0, 0, LWA_ALPHA );   
    SetFocus( applicationWindow_ );
}

After I call f(), the function goes through Section 1, branches into the if statement, completes line 1 (psFrame->...) and returns after that without evaluating the remaining two lines and the Section 2 out of the branch. I had this happen to me when for instance I was calling a method of a variable which was NULL, in this case psFrame, and it would instead of breaking, just return. However it is a legitimate variable, its contents and the pointer returned from getWindow() exists. In fact I can trace until the completion of deactivate() however my breakpoint at the next line is never hit.

In fact this is the same code that runs on one computer, and doesn't on my laptop. Both running Win 7.

What do you think could be the cause of this?


It sounds like something (quite possibly the deactivate, or something it calls) is making a mess of the stack (e.g., overwriting the end of a buffer) and messing up the return address. Much more than that would be a pretty wild guess though.


Given your description, it still sounds like you are getting null dereference errors. Guard your code a bit and see what happens like this:

if( interactFrame )
{
    if (psFrame)
    {
        if (psFrame->getWindow())
        {
            psFrame->getWindow()->deactivate();
        }
        // else log("getWindow == null")
    }
    // else log("psFrame == null")
    interactFrame = activeFrame = 0;
    logFile << "PS deactive" << endl;
}        

Beyond that we'd need to see more code.

UPDATE: OK - you posted more code, and that's pretty odd, unless something very strange is happening like getWindow() is overrunning your stack and trashing the return address. Check any local variables (especially strings and arrays) you have in getWindow().

GMan also has a good point - if psFrame is returning a pointer to a deleted window in getWindow, that could also be a culprit (and you might see different behaviors depending on if the memory has been re-allocated or not yet)


I guess the line

    psFrame->getWindow()->deactivate();

simply generates an exception. And your function does not return at all - it terminates with exception. To confirm that set a breakpoint after the call to f() function (part of which is the code you've posted) and if this breakpoint doesn't hit either then it is likely an exception (possibly invalid memory access or simply C++ exception thrown).

Stack corruption is also possible and it will also likely lead to an exception (unless you accidentally overwrite return address with a valid address to executable memory).

Also note that if psFrame happen to be 0 (or other invalid pointer) then exception is guaranteed if getWindow() access any non-static member of its object in any way. And you would see exactly the behaviour you described. The same situation is when psFrame->getWindow() returns 0 (or another invalid pointer) and deactivate() accesses non-static member.

UPD:

You may also follow stack contents changes when debugging.

0

精彩评论

暂无评论...
验证码 换一张
取 消