Creating STL exception leads to segfault

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
atomaki
Posts: 7
Joined: Tue Mar 22, 2016 5:56 am

Creating STL exception leads to segfault

Post by atomaki »

Hi everyone! This is my first message on the forum.

When I add std::runtime_error("err") expression into my code, I got segmentation fault at address 0 (before entering to main). If instead I use std::exception(), app runs fine.

How std::runtime_error() causes segfault and how to fix it? Thanks!

Code: Select all

 
#include <stdexcept>
#include <exception>
#include "irrlicht.h"
using namespace irr;
int main() {
    IrrlichtDevice* device = createDevice();
    if (!device)
        throw std::runtime_error("err");  //without this run fine
}
 
Compile:

Code: Select all

 
g++ -ggdb -Wall -Wextra -std=c++11 -o main main2.cc \
-lIrrlicht -lGL -lXxf86vm -lXext -lX11 -lXcursor
 
Backtrace:

Code: Select all

 
(gdb) bt full
#0  0x00000000 in ?? ()
No symbol table info available.
#1  0xb5d88fe1 in init () at dlerror.c:177
No locals.
#2  0xb5d8942e in _dlerror_run          (operate=operate@entry=0xb5d88e10 <dlsym_doit>,     args=args@entry=0xbffff160) at dlerror.c:129
    result = <optimized out>
#3  0xb5d88e98 in __dlsym (handle=0x0, name=0xb7f8afe5  "posix_memalign") at dlsym.c:70
    args = {handle = 0x0, name = 0xb7f8afe5 "posix_memalign", who = 0xb7f674bc, sym = 0xb5d65b20}
    result = <optimized out>
#4  0xb7f674bc in ?? () from /usr/lib/nvidia-   304/libGL.so.1
No symbol table info available.
Backtrace stopped: previous frame inner to this frame     (corrupt stack?)
 
LD debug

Code: Select all

 
LD_DEBUG=all LD_DEBUG_OUTPUT=log.txt ./main
tail log.txt.*
 
   3848:    symbol=__pthread_key_create;  lookup in file=/lib/i386-linux-gnu/libgcc_s.so.1 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/lib/i386-linux-gnu/libc.so.6 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/usr/lib/nvidia-304/tls/libnvidia-tls.so.304.131 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/usr/lib/nvidia-304/libnvidia-glcore.so.304.131 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/usr/lib/i386-linux-gnu/libXext.so.6 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/lib/i386-linux-gnu/libdl.so.2 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/usr/lib/i386-linux-gnu/libxcb.so.1 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/lib/ld-linux.so.2 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/usr/lib/i386-linux-gnu/libXau.so.6 [0]
  3848: symbol=__pthread_key_create;  lookup in file=/usr/lib/i386-linux-gnu/libXdmcp.so.6 [0]
 
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Creating STL exception leads to segfault

Post by hendu »

It clearly says the crash comes from your Nvidia driver. Report to Nvidia.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Creating STL exception leads to segfault

Post by mongoose7 »

It's not clear at all; the stack is corrupt. I would say it is not a Nvidia problem; the driver is written in C. Maybe you have a memory problem.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating STL exception leads to segfault

Post by CuteAlien »

I tried to reproduce it, but doesn't crash here. Sorry, not seen that specific problem before.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
atomaki
Posts: 7
Joined: Tue Mar 22, 2016 5:56 am

Re: Creating STL exception leads to segfault

Post by atomaki »

I've checked RAM with memtest86 : no errors found.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating STL exception leads to segfault

Post by CuteAlien »

I suspect it's probably some kind of linker trouble causing this. Something wrong libs something...
Check if it also happens if you don't use any Irrlicht code at all. Or don't even link to Irrlicht.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Creating STL exception leads to segfault

Post by mongoose7 »

atomaki wrote:I've checked RAM with memtest86 : no errors found.
Colloquially, a memory error is an error relating to allocating memory in a program. Errors occur where you a) free the same memory twice, b) free memory that was never allocated, or c) overwrite memory. Buffer overuns are the most common cause of memory issues.

Of course, it may also be a linking problem. C++ is pretty fragile regarding linking of dynamic objects (DLLs).
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating STL exception leads to segfault

Post by CuteAlien »

It crashes before main... I don't think it's about de-allocating memory twice.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
atomaki
Posts: 7
Joined: Tue Mar 22, 2016 5:56 am

Re: Creating STL exception leads to segfault

Post by atomaki »

If I'm not using any Irrlicht facilities, application runs OK (without segfaults)

Code: Select all

 
#include <stdexcept>
#include <exception>
#include "irrlicht.h"
 
int main() {
    throw std::runtime_error("err");
}
 

Code: Select all

 
$ g++ -g -O0 -Wall -Wextra -std=c++11  -o main main2.cc \
         -lIrrlicht -lGL -lXxf86vm -lXext -lX11 -lXcursor 
 
 

Code: Select all

 
$ ./main 
terminate called after throwing an instance of 'std::runtime_error'
  what():  err
Aborted (core dumped)
 
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating STL exception leads to segfault

Post by CuteAlien »

Very confusing. Are you using an Irrlicht from a distributor? In that case maybe download it fresh from our website. And then start by compiling the examples (just call ./buildAllExamples.sh in the examples folder). If those run, then try adding the runtime_error stuff to one of them.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
atomaki
Posts: 7
Joined: Tue Mar 22, 2016 5:56 am

Re: Creating STL exception leads to segfault

Post by atomaki »

Irrlicht downloaded from our website:
http://irrlicht.sourceforge.net/?page_id=10

version (1.8.3)
I've compiled and run examples without any errors.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating STL exception leads to segfault

Post by CuteAlien »

Running out of ideas. Maybe check with ldd if it really links to the libs you expect it to link to (ldd yourappname). You can also copy/paste the results in here in case anything looks suspicious to one of us.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
atomaki
Posts: 7
Joined: Tue Mar 22, 2016 5:56 am

Re: Creating STL exception leads to segfault

Post by atomaki »

Code: Select all

 
$ ldd main
    linux-gate.so.1 =>  (0xb7727000)
    libGL.so.1 => /usr/lib/nvidia-304/libGL.so.1 (0xb7628000)
    libXxf86vm.so.1 => /usr/lib/i386-linux-gnu/libXxf86vm.so.1 (0xb7622000)
    libX11.so.6 => /usr/lib/i386-linux-gnu/libX11.so.6 (0xb74ed000)
    libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xb7405000)
    libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb73bf000)
    libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb73a2000)
    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb71f4000)
    libnvidia-tls.so.304.131 => /usr/lib/nvidia-304/tls/libnvidia-tls.so.304.131 (0xb71ef000)
    libnvidia-glcore.so.304.131 => /usr/lib/nvidia-304/libnvidia-glcore.so.304.131 (0xb54e9000)
    libXext.so.6 => /usr/lib/i386-linux-gnu/libXext.so.6 (0xb54d6000)
    libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb54d1000)
    libxcb.so.1 => /usr/lib/i386-linux-gnu/libxcb.so.1 (0xb54af000)
    /lib/ld-linux.so.2 (0xb7728000)
    libXau.so.6 => /usr/lib/i386-linux-gnu/libXau.so.6 (0xb54aa000)
    libXdmcp.so.6 => /usr/lib/i386-linux-gnu/libXdmcp.so.6 (0xb54a3000)
 
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating STL exception leads to segfault

Post by CuteAlien »

Is this a 32-bit system? Maybe I should try reproducing on that (got to admit I haven't tested on 32-bit in a rather long time...).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Creating STL exception leads to segfault

Post by hendu »

For giggles, link your standalone program that works with -lGL and see if it starts to fail.
Post Reply