Using shared_ptr and glutInit causes segmentation fault

北城以北 提交于 2020-01-03 07:17:34

问题


Having asked this before I tried out a lot of things and found out that the problem has to do with glutInit. Take the following code examples: main.cpp

#include <iostream>
#include <memory>
#include<GL/glut.h>
using namespace std;
int main(int argcp, char **argv)
{
    shared_ptr<double> abc;
    glutInit(&argcp,argv);
    cout<<"Hello!"<<endl;
    return 0;
}

compiled with:

g++ -std=c++11 -g -Wall -o appx main.cpp -lGL -lGLU -lglut

cause the executable to instantly crash (no "Hello!" output) with segfault using g++ 5.2.1, ubuntu 15.10

Just commenting out the line

shared_ptr<double> abc;

will fix the crash.

Since i want to use shared_ptr and glut in a project I would like to know how this can be fixed or what causes the crash.

Edit 1: GDB trace:

#0  0x0000000000000000 in ?? ()
#1  0x00007ffff33fb6fd in init () at dlerror.c:177
#2  _dlerror_run (operate=operate@entry=0x7ffff33fb0e0 <dlsym_doit>,args=args@entry=0x7fffffffde00) at dlerror.c:129
#3  0x00007ffff33fb148 in __dlsym (handle=<optimized out>, name=optimized out>) at dlsym.c:70
#4  0x00007ffff6fa2e1e in ?? () from /usr/lib/nvidia-352/libGL.so.1
#5  0x00007ffff6f4db47 in ?? () from /usr/lib/nvidia-352/libGL.so.1
#6  0x00007ffff7de957d in call_init (l=0x7ffff7fc59c8,argc=argc@entry=1,         argv=argv@entry=0x7fffffffdf58, env=env@entry=0x7fffffffdf68)at dl-init.c:58
#7  0x00007ffff7de96cb in call_init (env=<optimized out>, argv=<optimized out>, argc=<optimized out>, l=<optimized out>) at dl-init.c:30
#8  _dl_init (main_map=0x7ffff7ffe188, argc=1, argv=0x7fffffffdf58, env=0x7fffffffdf68) at dl-init.c:120
#9  0x00007ffff7dd9d0a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#10 0x0000000000000001 in ?? ()
#11 0x00007fffffffe2c8 in ?? ()
#12 0x0000000000000000 in ?? ()

回答1:


As posted in the comments the problem was similar to the problem posted here.

The solution is adding:

 -lpthread

to the compiler flags! Thanks a lot!



来源:https://stackoverflow.com/questions/33857489/using-shared-ptr-and-glutinit-causes-segmentation-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!