I have a simple application.
@int main(int argc, char *argv)
Sleep(10); // <windows.h> sleep
I add breakpoints. They are hit/not hit depending on its position and sleep delay:
- On line 2 - never stops
- On line 4 - stops when sleep is at least 50 ms. When Sleep(10), doesn't stop.
That means, I'm not able to debug my applications for first 50 ms. Why and how to fix it better than adding delay at the beggining of my application?
Qt Creator 2.4.1
Your application seems a bit strange to begin with. The optimizer will probably remove all your code because nothing is done! Then the debugger doesn't hold a PC pointer value to stop at, so it will not stop at all.
First try to add some lines of code and place breakpoint on them. When they don't get hit, then you have a problem.
I think the optimizer can not optimize the Sleep() away, because it cannot know which side-effects that call has! It might be able to optimize away an inline function that really has no side-effects. But Sleep() is a system call (Win32 API) where the source code (implementation details) are not publicly available and may even change with future Windows versions. Especially the compiler would not do this kind of optimizations in a DEBUG build, where optimizations are disabled (or at least reduced to a minimum).
It's pretty strange that the debugger will only catch the break point when the Sleep() is long enough! My theory: The IDE first starts the process, then attaches the debugger. When breakpoint is reached (triggered) before a debugger is attached to the process, nothing will happen!
Definitely can not reproduce such behavior with MSVC, breaks as expected:
What IDE do you use?
I use Qt Creator 2.4.1, Qt 4.8.0, MinGW 4.4, Windows 7 32 bit.