Something about qt creator debug
-
If I have a code like this
@void testFun()
{
int result = add(a, b);
}@
The value of 'result' cannot be watched when debugging. the qt creator hint me , that the variable 'result' is out of scopt when the program run to the end of this function. Consider this situation, I have to add a meaningless sentence to keep the 'result' still in scope and watch the 'result' value. Is this gdb problem or qt creator. How can I resolve it? The same code in msvc, I can see the 'result' value no doubt. This is not convinient for debugg. -
It is most likely neither the fault of creator nor gdb: The compiler will be optimizing out the variable. I am surprised that MSVC is supposedly not doing this.
Try disabling optimizations in your compiler. You can also add a Q_UNUSED(result); after line 3 to discourage the compiler from using this optimization.
-
Have you set a breakpoint in line 3? After leaving the function result is destroyed, there is nothing that can be watched afterwards.
Or I do not get, what you really asked for ;-) And what is a and b? -
Yes, you didn't get me . what is a and b is not the problem. the code I wrote is just a sample. The problem is , if a temporary variable declared at the end of a function. you will not able to watch the value of it when you debug the function in qt creator.
[quote author="hardcodes.de" date="1368782030"]Have you set a breakpoint in line 3? After leaving the function result is destroyed, there is nothing that can be watched afterwards. Or I do not get, what you really asked for ;-) And what is a and b?[/quote]
-
if my programe is released, the variable may be optimized out. But what I said is in debug mode, In my opinion , there is no optimization in debug mode. the macro Q_UNUSED doesn't work as I expected. I still can't watch the value of te variable at the end of function.
[quote author="Tobias Hunger" date="1368782018"]It is most likely neither the fault of creator nor gdb: The compiler will be optimizing out the variable. I am surprised that MSVC is supposedly not doing this. Try disabling optimizations in your compiler. You can also add a Q_UNUSED(result); after line 3 to discourage the compiler from using this optimization.[/quote]
-
I'm not sure what's going on with your GDB/Creator, but here's a different method you can try:
@
#include <QDebug>void testFun()
{
int result = add(a, b);
qDebug() << result;
}
@ -
Yes , qDebug is a good way. I want is not only a solution, I am interested in why this happen?
[quote author="JKSH" date="1368789354"]I'm not sure what's going on with your GDB/Creator, but here's a different method you can try: @ #include <QDebug> void testFun() { int result = add(a, b); qDebug() << result; } @[/quote]
-
[quote author="henryxuv" date="1368789831"]Yes , qDebug is a good way. I want is not only a solution, I am interested in why this happen?[/quote]I don't know the answer; you'll have to ask someone else :) "Stack Overflow":http://stackoverflow.com/ is a good place to ask general software development questions. I think yours is a GDB issue, not a Qt Creator issue.
-
Maybe if you dump the generated object file, you can reproduce if the lines are actually there or optimized away.
-
You are right, the variable has been optimizated out. I try to disable the optimization, but failed. Could you tell me a way to do that, I check my qmake.conf file under qt\mkspecs\win32-g++ directory. (because my platform is winxp). the QMAKE_CFLAGS is empty. without any flags for optimization.
[quote author="Tobias Hunger" date="1368782018"]It is most likely neither the fault of creator nor gdb: The compiler will be optimizing out the variable. I am surprised that MSVC is supposedly not doing this. Try disabling optimizations in your compiler. You can also add a Q_UNUSED(result); after line 3 to discourage the compiler from using this optimization.[/quote]
-
Maybe the volatile keyword can help here although I'm not sure that the creators of C++ had that in mind.
Just out of curiosity: what is the goal of the whole thing?
-
When I wrote program with Qt Creator, I find the situation which never happened in MSVC. So I wonder why? For me, this is a opportunity to know Qt and it's tools in detail. So I try to figure out the reason. Thank for all of you to help me .Now I have got the reason.
[quote author="hardcodes.de" date="1369330298"]Maybe the volatile keyword can help here although I'm not sure that the creators of C++ had that in mind.Just out of curiosity: what is the goal of the whole thing?[/quote]