Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-01-2008, 12:53 PM   #1
Registered: Aug 2007
Location: Canada
Distribution: OpenBSD 4.6, Debian Lenny
Posts: 64

Rep: Reputation: 16
SDL segmentation fault and image corruptions

Basically I have two problems. I'm currently developing a networked pong game using SDL and C with my class at college. It has the functionality to support up to 16 players in a single game, but issues are arising which causes the program to crash at random times and some images get messed up when new players join.

Okay first the program crashes randomly after like 5 minutes to 1 hour when SDL_UpperBlit is called like so:
#define MAX_BLITS	34

struct blit {
        SDL_Surface *src;
        SDL_Rect *srcrect;
        SDL_Rect *dstrect;
} blits[MAX_BLITS];

void update_screen(void)
        int i;
        for (i = 0; i < numupdates; ++i) {
		if (blits[i].src == 0 || blits[i].srcrect == 0 ||
			screen == 0 || blits[i].dstrect == 0) {
                SDL_UpperBlit(blits[i].src, blits[i].srcrect, screen,
        SDL_UpdateRects(screen, numupdates, dstupdate);
        numupdates = 0;
The design of the program is that each client loads a ball image, two white paddles (horizontal and vertical) and one red paddle they control (horizontal or vertical). The server assigns players a side based on the order they join. Each player sends its paddle to the server. The server sends all paddles out to the players and they assign the appropriate image to draw based on the index in an array like so:
if (ball.alive) {
        for (j = 0; j < players; j++) {
                if (j != get_client_pos()) {
                        p = get_paddle(j);

                        if (j == 0 || j == 1 || j == 4
			|| j == 5 || j == 8 || j == 9
			|| j == 12 || j == 13)
                                p.image = *other_vert;
                                p.image = *other_horz;

                        if (p.alive)

                        tmp[j] = p;
        send_paddle(player, server_ip);
The problem is when a third person joins the vertical white paddle on player one and player two's opposite side shrinks in height to the point where it is as small as the ball. And the third person sees all paddles fine.

Anyways any suggestions towards either problem would help greatly.
Old 04-01-2008, 03:37 PM   #2
Senior Member
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
oowwe, my eyes.... sooo many hard coded numbers, and all packed together without comments... Not that it matters, doesn't seem to be anything interesting in that code anyways, nothing i can see to cause a error.

Have you tried just stepping thru program in a debugger like ddd? First, i would recommend a good pass thru valgrind, seems like a memory problem is a good place to start.
Old 04-01-2008, 04:28 PM   #3
Registered: Aug 2007
Location: Canada
Distribution: OpenBSD 4.6, Debian Lenny
Posts: 64

Original Poster
Rep: Reputation: 16
I've just started running valgrind on it. From what it seems about 3/4 of the memory allocated is not being freed. The output of any real reference to the update_screen() function was:
==25943== 2,204 bytes in 1 blocks are still reachable in loss record 148 of 186
==25943==    at 0x4A05AF7: realloc (vg_replace_malloc.c:306)
==25943==    by 0x34D7015510: (within /usr/lib64/
==25943==    by 0x34D701673B: (within /usr/lib64/
==25943==    by 0x34D702A465: (within /usr/lib64/
==25943==    by 0x34D702B61F: SDL_LowerBlit (in /usr/lib64/
==25943==    by 0x34D702B861: SDL_UpperBlit (in /usr/lib64/
==25943==    by 0x403B80: update_screen (in /root/Desktop/pong_version_9_laverne/client)
==25943==    by 0x403373: game (game.c:202)
==25943==    by 0x403994: game_start (game.c:52)
==25943==    by 0x402A79: on_start_button_clicked (main_window.c:267)
==25943==    by 0x34C6C0B1A8: g_closure_invoke (in /lib64/
==25943==    by 0x34C6C1A830: (within /lib64/
And the summary was:
==25943== LEAK SUMMARY:
==25943==    definitely lost: 18,404 bytes in 51 blocks.
==25943==    indirectly lost: 28,192 bytes in 881 blocks.
==25943==      possibly lost: 164,704 bytes in 889 blocks.
==25943==    still reachable: 2,269,132 bytes in 17,251 blocks.
==25943==         suppressed: 0 bytes in 0 blocks.
Sorry about the code quality here, were only second year students, so we don't have a whole lot of actual experience working on bigger projects.
Old 04-01-2008, 04:52 PM   #4
Senior Member
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Ummm, just to be clear, you recreated the error in valgrind? Because its nice to have memory friendly code and all, but using valgrind instead of a debugger is so its possible to see if memory is getting corrupted around the time of the error.


fault, sdl, segmentation fault

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
Cube fails to run with segmentation fault (SDL parachute deployed) bomarrow1 Linux - Games 11 09-23-2006 10:25 AM
Fatal signal: Segmentation Fault (SDL Parachute Deployed) avis Linux - Software 1 05-10-2005 11:40 AM
Segmentation fault(SDL parachute deployed) DropSig Linux - Software 0 06-12-2004 12:28 PM
frozen bubble Segmentation Fault (SDL Parachute Deployed) lrt2003 Linux - Games 7 04-18-2004 04:48 PM
SDL-Quake, Segmentation fault, Fullscreen and LAN-problems... Jonthebest Linux - Software 0 09-13-2003 10:45 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:24 PM.

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