简体   繁体   中英

Triggering a breakpoint in a function that is not called

This is a weird one. I have a function on a class which has a breakpoint in it, that is being hit even though the rest of the function is not running. The amount of code it would take to reproduce this is probably impractical to post here, but this is what I'm seeing:

MyClass.h:

enum OptionsEnum { OPTION_1, OPTION_2 };

struct OptionsStruct
{
    OptionsEnum options;
    int value;
};

class MyClass
{
private:
    Initialize(...);
    Process(const OptionsStruct&);

    OptionsStruct m_Options { };
}

MyClass.cpp:

#include "MyClass.h"

void MyClass::Initialize(...)
{
    ...do some stuff with local variables... //<--Breakpoint here not triggered
    ...do some more stuff...

    Process(m_Options); //<--Breakpoint here is triggered!  How?!

    ...do yet more stuff... //<--Breakpoint here not triggered
}

void MyClass::Process(const OptionsStruct& options)
{
    ...do some other stuff... //<--Breakpoint here not triggered
}

I've seen plenty of posts which have the opposite problem where a breakpoint they were expecting to be hit was optimized out and thus never triggered, but I can't think of how the reverse would be possible: for optimization to cause a breakpoint to be hit in the middle of a function which was never called in the first place.

Stepping the code through shows that it never actually enters the Initialize() function, but when run with the breakpoints, it triggers it. To be sure, I even turned off optimizations and let it run, and it still hits that breakpoint. And only that breakpoint. I put a breakpoint on every single line of code in the function, and only that one hits. The breakpoint in Process() also doesn't trigger, even though it's called by the very line that did trigger.

The pattern seems to be that only lines which have member variables in them get hit. And when they do, if you mouse over m_Options , all of the fields in the struct are junk (uninitialized) values.

The callstack is not helpful - it just points to [External Code] and below that [Frames below may be incorrect and/or missing...] The function is private, so I don't know how it's even possible for external code to be calling it. Is this a bug in the debugger? Optimization wizardry of some sort? What could possibly make something like this happen?

Memory corruption could cause the problem. Maybe somewhere in your code you have a corrupted pointer and it may be a side-effect of using it. It would partially explain why you can't acces callstack. Just to be sure... Is the breakpoint in exactly the same source code that you run? It is possible to debug even if the code differs from the compiled version. It would make breakpoints useless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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