How can glfwSleep () cause a segfault?
in my multitasking application, I am using the sleep () function (one from the GLFW library):
glfwSleep(..);
and this appears to cause my application to segfault as my call stack shows:
#0 76CFC2BC WaitForSingleObjectEx() (C:\Windows\system32\kernel32.dll:??)
#1 00000016 ??() (??:??)
#2 0000006C ??() (??:??)
#3 00000000 ??() (??:??)
glfwSleep()
used inside a stream. This is dangerous? Why is my program undergoing major changes?
Edit:
When the parameter glfwSleep()
is <0.02 (secs) it is not a segfault!
Edit 2:
From GLFW official documentation:
Writing threaded apps can be very awkward before getting used to it, but there are a few key rules that are simple enough:
- ALWAYS provide exclusive access to data that is shared by threads!
- Make sure the streams are synchronized correctly.
- NEVER wait!
I think I got my answer. Now find an alternative.
Thanks!
a source to share
To quote the Pragmatic Programmer ,
`` select Isnt Broken
It's rare to find a bug in the OS or compiler, or even a third party product or library. The probability of an error in the application.
Why does your program call WaitForSingleObjectEx()
when it glfwSleep()
calls Sleep()
? Well, even if you don't have the source code Sleep()
, it's not a completely black box. Parse Sleep()
it and you will probably see (depending on which version of Windows you have) that Sleep()
either calls or tail-calls SleepEx()
. In XP, it SleepEx()
calls NtDelayExecutionThread()
, and in Vista it calls WaitForSingleObjectEx()
.
So what happened to the rest of your stack? 00000016, 0000006C and 00000000 are invalid return addresses. I wouldn't be surprised if, somewhere in your code, you are passing a pointer to a stack-allocated buffer to another thread, and while your program is sleeping, that other thread mangles the first stack of the thread. Step in Sleep()
, place a memory breakpoint on the return address and you can catch the culprit.
a source to share