OpenAL Segmentation Fault Before Main - c++

I'm writing a game engine in C++ that utilizes SDL for graphics and OpenAL for audio. I'm developing on Windows XP compiling with MinGW and debugging with GDB. I discovered the problem when I ran the debugger and, before even reaching main(), the program received a Segmentation Fault. I was (and still am) confused why a segfault would occur before the program reached main. However, even though GDB said it received SIGSEGV, the program continued happily and ran exactly as I would expect it to (displayed graphics, played sounds, music, etc.)
Of course, being a game engine, there are a lot of different components, so I got rid of all components (excluded them from compiling or commented them out) and added them back in until I discovered that my calls to OpenAL were causing the segfault. Oddly enough though, the segfaults still occurred before main, and commenting out the OpenAL function calls subsequently caused the segfault to stop occurring on the next run.
So, to test out my hypothesis, I made a separate project with minimal code and linked only with OpenAL32, and voila! Segfault. This is literally the code in its entirety:
#include <al.h>
int main()
{
alGetError();
return 0;
}
I can replace alGetError() with any other OpenAL function call (even alcOpenDevice(), which is designed to be the first library call), and the segfault occurs. Remove alGetError(), and the segfault disappears.
The backtrace directly at the point of the segfault shows the following:
Program received signal SIGSEGV, Segmentation fault.
0x7c918af2 in ntdll!RtlpWaitForCriticalSection ()
from C:\WINDOWS\system32\ntdll.dll
(gdb) bt
#0 0x7c918af2 in ntdll!RtlpWaitForCriticalSection ()
from C:\WINDOWS\system32\ntdll.dll
#1 0x7c901046 in ntdll!RtlEnumerateGenericTableLikeADirectory ()
from C:\WINDOWS\system32\ntdll.dll
#2 0x00e461a0 in ?? ()
#3 0x77dd6cd8 in RegCloseKey () from C:\WINDOWS\system32\advapi32.dll
#4 0x77dde88d in RegCreateKeyExA () from C:\WINDOWS\system32\advapi32.dll
#5 0x77de473f in RegCreateKeyA () from C:\WINDOWS\system32\advapi32.dll
#6 0x6650127a in ?? () from C:\WINDOWS\system32\wbsys.dll
#7 0x7c90118a in ntdll!LdrSetAppCompatDllRedirectionCallback ()
from C:\WINDOWS\system32\ntdll.dll
#8 0x66500000 in ?? ()
#9 0x7c91c342 in ntdll!LdrHotPatchRoutine ()
from C:\WINDOWS\system32\ntdll.dll
#10 0x7c915c69 in ntdll!RtlValidateUnicodeString ()
from C:\WINDOWS\system32\ntdll.dll
#11 0x7c915dcb in ntdll!LdrShutdownProcess ()
from C:\WINDOWS\system32\ntdll.dll
#12 0x00000000 in ?? ()
RtlpWaitForCriticalSection() makes it seem like a thread issue, which OpenAL runs in a separate thread if I'm not mistaken. However I'm not too familiar with multithreaded programming.
I compiled my project (the whole game engine, in its current state) under Linux and no segfault message occurs, and the program runs as I would expect it to (this means there are a couple other errors/segfaults that I still have to fix up but I have a handle on them :P).
I'm not sure exactly what my question is. I guess I'll leave it with this question: is the segfault signal I'm receiving a result of the way I'm implementing/linking OpenAL, or is it something beyond my control that the OpenAL library causes? I would assume it's not a problem with OpenAL itself, considering the broad user base of the library, but who knows. Maybe it's a problem with the fact that I am linking my C++ project to a C library?
UPDATE : I've been researching this for a few days now, and I'm getting very frustrated. The only results I can find for "Segfault before main" are: "You have a static constructor, which is called before main" and "Oh, it actually wasn't before main. I need to learn how to use a debugger."
Well, I've never heard of static constructors in C++. And anyway, I reproduced the error without any classes to begin with. And I only run into the segfault signal when I'm using the debugger, because the program runs perfectly if it just blasts through the signal (a.k.a continuing). Putting a breakpoint on the first instruction in main or even on main(), the segfault occurs before the breakpoint is hit.

Related

gdb reports Segmentation fault - how to know where?

I'm running my program under gdb, with debuging information and without any optimizations. gdb reports:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffeffff700 (LWP 8875)]
0x0000001000000001 in ?? ()
From this message i do not understand where the problem happened. Is it possible to extract stacktrace / problem file and line number?
To get the point where code segmentation fault has happened, you should use backtrace (bt) command.
There are wide range of commands available inside gdb which should be explored to help make you debug your code as efficiently as possible.
e.g. you could record your code flow and replay it in reverse.
explore data types
have breakpoints etc.

what is the reason for core dump? stack shows from oracle lib

I have a coredmp where all thread stack look normal but one stack shows like this. Can any one tell me possible reason for this? i can see exit is being called from oracle libs, is this issue of oracle? Can any one guide me when this can happen?
Thread 3 (process 26454):
#0 0x00002b803ceb54a8 in exit () from /lib64/libc.so.6
#1 0x00002b803bbe93f5 in skgdbgcra () from /home/oracle/product/11g/lib/libclntsh.so.11.1
#2 0x00002b803be9cdec in kpeDbgCrash () from /home/oracle/product/11g/lib/libclntsh.so.11.1
#3 0x00002b803be9c627 in kpeDbgSignalHandler () from /home/oracle/product/11g/lib/libclntsh.so.11.1
#4 0x00002b803bbe64b1 in skgesig_sigactionHandler () from /home/oracle/product/11g/lib/libclntsh.so.11.1
#5 <signal handler called>
> Program terminated with signal 6, Aborted.
Note:
Before voting down, can you guys tell me why voting down? I am really clueless as what to check
Here is another stackoverflow link where stack is similar but not same, but still no clear answer
Link talks about orable bug, Is this same isue?
Looks like it's an issue on oracle 11g.
Starting from Oracle 11g Diagnostic Repositories are turned on by default. Automatic Diagnostic Repository (ADR) Parameters such as DIAG_SIGHANDLER_ENABLED, DIAG_ADR_ENABLED, DIAG_DDE_ENABLED are mostly set at SQLNET.ORA.
Having DIAG_SIGHANDLER_ENABLED will force all diagnostics to be written such as alert logs, trace files, application dumps and that becomes a overload and sometimes the Application stumbles.
To turn off the signal handler and re-enable standard Operating System failure processing, place the following parameter setting in your client side or server side sqlnet.ora file under $ORACLE_HOME/network/admin directory.
DIAG_ADR_ENABLED=OFF
DIAG_SIGHANDLER_ENABLED=FALSE
DIAG_DDE_ENABLED=FALSE
By default this parameter is set to true.
After making any such changes to ADR ie., turning off DIAG_SIGHANDLER_ENABLED, DIAG_ADR_ENABLED, DIAG_DDE_ENABLED, it is recommended to restart the Application after making the setting in sqlnet.ora.
Turning off ADR parameters should cause no impact either to your Application or Database.

Any safe cross-platform way of writing variables into executable file?

Let's say, I want to create a single-file game with one variable - 6 digit highscore. I think that the process of saving the variable would be something like this:
Main program creates datachanger.exe and dataholder.txt
Main program launches datachanger.exe
datachanger.exe is constantly checking wether main program process is still alive
Main program closes
datachanger.exe opens the dead main program, deletes last 6 symbols, inserts new ones instead
datachanger.exe launches an independent console script that kills it (suicidal maneuver) and deletes together with dataholder.txt
When script is over, it's over. We have main program with a new variable hiding inside.
I see no problem with steps #1, #4, #5 and #7. However, steps #2, #3 and #6 are problematic.
I can easily make them run with system(), but we all know the obvious downsides of this choice. CreateProcess() works only on Windows, so does GetExitCodeProcess() for the step #3. I don't know any solution besides system() for step #6, even that works reliably only on Windows.
Do I absolutely have to find out the OS user is using before the variable saving procedure?
What's the function for step #6 that works reliably and doesn't require the call of system() on Windows? Maybe it's possible to reduce the amount of steps required or simplify them?
I want to make my little games extremely user friendly.

How does gdb attach to multi-threaded process?

I will try to be as specific as I can, but so far I have worded this problem so poorly that Google failed to return any useful results (hence my question here).
I am attaching gdb to a multi-threaded c++ server process. All I can say is that strange things have been happening while trying to do the usual set-breakpoint-break-investigate.
First, while waiting for the breakpoint to be hit (in 'Continuing' mode), I suddenly got back the (gdb) prompt with the message:
Continuing.
[Thread 0x54d5b940 (LWP 28503) exited]
[New Thread 0x54d5b940 (LWP 28726)]
Cannot get thread event message: debugger service failed
Second, also while waiting for the breakpoint to be hit, I'm suddenly told the program has received SIGSEGV and - back to the (gdb) prompt - backtrace tells me the segfault happened in pthread_cancel(). Note the process under investigation does not normally segfault.
I clearly lack enough information about how gdb works to even begin guessing what is happening. Am I doing anything wrong? The steps I take are the same each time:
gdb attach
break 'MyFunction()'
continue
Thoughts? Thanks.
I fought with similar gdb issues for a while. My case was having lots of threads spawned that executed few functions and then exited.
It appears if a thread exits too fast and there's lots of these happening sometimes gdb cannot keep up and when it fails, it fails with style as in crashes :) I think it tries to attach to a thread that is already done as per the error message.
I see this as an issue in gdb 6.5 to 7.6 and still happening. Did not try with older versions.
My advice is look for this use case or similar. Once I changed my design to have a thread serving a queue of requests gdb works flawlessly.
Design wise is healthier to have already created threads that digest actions than always spawning new threads.
Still same code debugs without a problem on Visual Studio so I do have to say that is a small disappointment to me with regards to gdb.
I use Eclipse and looking at the GDB traces (usually enabled by default) will give you a better hint of where GDB fails. One of the buttons on the console shows you the GDB trace.

What is gdb/dbx doing when ddd is "waiting for it to get ready"?

I use ddd as a front-end for both gdb and dbx for C++ programs.
Quite often, without any apparent cause, I will try to next and it will hang with the message "Waiting for gdb to get ready" or "Waiting for dbx to get ready".
Does anybody know what it is that they're doing that takes forever and produces no apparent results? And can I stop it from happening?
Bear in mind that enough stuff has already been loaded that I have quite happily been stepping/nexting a minute earlier in the same process (and in the same function), so whatever they're doing doesn't seem to have been necessary for that. Also the fact that both ddd and dbx have the same pattern of behaviour (in many different executables and on different platforms) makes me think it's something in the data rather than a bug in either debugger.
GDB (and the same applies for DBX) communicate with DDD with the MI protocol, which is a standardize and unambiguous equivalent of the command-line interface.
Remark: the default in my system (Fedora 15) seems to be that they communicate directly using the CLI, but I only noticed the problem you describe with --interpret=mi.
For instance, here are the respective output to get the thread list:
(gdb) info threads
Id Target Id Frame
2 Thread 0x7ffff7fd2700 (LWP 9191) "philosophers" 0x00000037dcc0b4c5 in pthread_cond_wait##GLIBC_2.3.2 () from /lib64/libpthread.so.0
1 Thread 0x7ffff7fd3720 (LWP 9182) "philosophers" 0x00000037dc8df461 in clone () from /lib64/libc.so.6
(gdb) -thread-info
^done,threads=[
{id="2",target-id="Thread 0x7ffff7fd2700 (LWP 9525)",name="philosophers",
frame={level="0",addr="0x0000000000400b31",
func="chopsticks_put",
args=[{name="i",value="0"}],
file="chopsticks.c",fullname="philosphers/chopsticks.c",line="70"},
state="stopped",core="2"},
{id="1",target-id="Thread 0x7ffff7fd3720 (LWP 9522)",name="philosophers",
frame={...},
state="stopped",core="1"
}],current-thread-id="3"
So what you will see in DDD will be quite similar to what is available in the CLI, only the 'presentation layer' is different.
From my experience, most of GDB commands are very fast, at least when they don't depend on the debuggee execution (like a next over a sleep(5)). So there are two possibilities for your problem:
a bug in the communication: for instance a ^done tag is missed by DDD or forgotten by GDB, so DDD waits in vain for the termination of its request
DDD asks GDB for a lot of data, like the definition of structures, function locations or memory contents, etc. (for instance because of the elements you want to watch), so it will take some time for the information to be computed by GDB and transferred to DDD.
At the bottom of DDD you have the GDB console. Try typing some GDB commands in there. If GDB responds correctly (my case) it means that DDD is not synchronized with GDB anymore. (DDD is getting old, 2009/02/11, and MI is extensively used by Eclise, so I think we know who has to be blamed...!)