Pregunta

So I've been fighting with the std::stack libraries for the last day or so. I'm trying to debug some code of mine, and rather than doing things by pen and paper, I'm using the eclipse debugger.

The first weird thing, is that if I have an expressions relating to the stack in the eclipse expressions window, the program will die as soon as I start it.

The other thing, is that the first time it ran, the debugger saw the stack fine. I could inspect its elements through the expressions window. But as soon as I pushed something onto the stack, the stack.top() method started returning an error in the expression window. Then if I try to step the program, it just dies. This isn't the behavior when its running without the debugger, so I'm really at a loss here.

Here's the problem code:

All I need to do is hit step over, and Eclipse opens a window from the stl_deque.h file, and then I hit step return and it dies.

int main() {
    std::stack<Cell> path;
    return 0;
}
¿Fue útil?

Solución

Your code is fine (at least, the part of your code you've shown us). The problem you're describing is simply that the Eclipse debugger crashes when you try to evaluate stack.top() in it. This is a bug in Eclipse (specifically in its built-in support for inspecting C++ container types), and it's not something you can easily fix yourself.

One possible workaround would be to add debugging functions to your own code; for example

const Cell *stackTop(const std::stack<Cell> *stk)
{ return &stk->top(); }

Then whenever you want to get at stack.top() in your debugging session, type in *stackTop(&stack) instead. Notice that this definition of stackTop avoids dealing with templates, inlining, references, and move-semantics — all of which are things with which I would expect the average debugger to have trouble — instead it's just a single non-templated function that takes a pointer and returns a pointer. It would take a very stupid debugger to get confused by this simple of a function.

If Eclipse can successfully inspect std::vector<Cell> without crashing, then you might consider rewriting your code to use vector instead of stack; this will likely be more efficient anyway.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top