LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-12-2011, 06:12 PM   #1
coralfang
Member
 
Registered: Nov 2010
Location: Bristol, UK
Distribution: Slackware, FreeBSD
Posts: 254

Rep: Reputation: 73
C++/SDL segfault when using SDL_Flip()


Hello, i have recently decided to start learning some c++ as with previous languages, i feel its easier to understand "how things work" by creating small games or animations.

I have come across this tutorial on SDL here:
http://cone3d.gamedev.net/cgi-bin/in...ls/gfxsdl/tut2

And here is the code i have put together after following that:
http://pastebin.com/JxjnaiM6 (for the sake of syntax highlighting )

And compiled with:
Code:
g++ main.cpp -o main `sdl-config --libs --cflags`
Now for the issue, i can't seem to fix this error:
Code:
% ./main                                           
SDL Drawing Test!
zsh: segmentation fault  ./main
When i run it under gdb, the output shows:
Code:
% gdb main 
GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/ricky/Devel/cpp-sdl/main...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/ricky/Devel/cpp-sdl/main 
[Thread debugging using libthread_db enabled]
SDL drawing test started!
[New Thread 0x7fffee678700 (LWP 23754)]
[Thread 0x7fffee678700 (LWP 23754) exited]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b719f8 in SDL_Flip () from /usr/lib/libSDL-1.2.so.0
(gdb)
The only place i have SDL_Flip() mentioned is inside DrawScene(), i tried commenting out that line, obviously the application only then shows a black screen.

Any help explaining why this is segfaulting would be awesome, thanks.
 
Old 04-12-2011, 06:16 PM   #2
coralfang
Member
 
Registered: Nov 2010
Location: Bristol, UK
Distribution: Slackware, FreeBSD
Posts: 254

Original Poster
Rep: Reputation: 73
Oh wow i found out i redeclared:
Code:
SDL_Surface *screen;
in main()

Removed it and now it works!

Solved i guess.
 
Old 04-12-2011, 06:41 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
GCC can warn you whenever you "shadow" a variable like this I forget the exact keyword, but it's included with -Wall -Werror options I always compile with (it's got plenty of other bug-catching goodies too).
 
1 members found this post helpful.
Old 04-12-2011, 06:59 PM   #4
coralfang
Member
 
Registered: Nov 2010
Location: Bristol, UK
Distribution: Slackware, FreeBSD
Posts: 254

Original Poster
Rep: Reputation: 73
I think i'll be using that all the time from now on:
Code:
% g++ main.cpp -o main `sdl-config --libs --cflags` -Wshadow
main.cpp: In function ‘int main(int, char**)’:
main.cpp:109:15: warning: declaration of ‘screen’ shadows a global declaration
main.cpp:29:14: warning: shadowed declaration is here
Very Helpful, Cheers.
 
  


Reply

Tags
cpp, sdl, segfault


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SDL is installed, included and linked, but will not compile SDL code mansizerooster Programming 10 05-31-2006 04:18 AM
Sourcecompiling Problem...The sdl-config script installed by SDL could not be found. deepclutch Debian 1 12-15-2005 12:15 PM
Trying to program an SDL application but cannot find the SDL.h file:SuSE 9.2&KDevelop pujolasdf Linux - Newbie 4 03-13-2005 07:50 AM
SDL_Flip() Vs SDL_UpdateRects() MadCactus Programming 1 10-10-2004 04:26 PM
SDL Error: /dev/snd/pcmC0D0p - "busy" = segfault tireseas Linux - Software 0 07-18-2004 11:35 AM


All times are GMT -5. The time now is 12:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration