Strage behavior in SDL_PollEvents

In a normal while loop for processing events a specific structure gets
trashed.

Can anyone explain why this could be happening:--------------------------

#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <stdlib.h>
#include <stdio.h>

struct particle {

 float x, vx;
 float y, vy;

};

int main() {

SDL_Event event;
int done=0;
struct particle falling_obj[2];

falling_obj[0].x=falling_obj[0].vx=0.0f;
falling_obj[0].y=10.0f;
falling_obj[0].vy=-98.0f;

falling_obj[1].x=falling_obj[1].vx=0.0f;
falling_obj[1].y=10.0f;
falling_obj[1].vy=-98.0f;

SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(800,600,24,SW_SURFACE | SW_FULLSCREEN | SW_OPENGL);

setup_opengl(); // Just the usual stuff…

while(!done) {

 fprintf(stderr, "y:%f vy:%f\n", falling_obj[1].y, falling_obj[1].vy);

 while(SDL_PollEvents(&event)) {
      if(event.type==SDL_KEYDOWN) {
           switch(event.key.keysym.sym) {
                case SDL_ESCAPE:
                fprintf(stderr, "ESC!\n\n");
                done=1;
                break;
           }
      }
 }

 fprintf(stderr, "y:%f vy:%f\n", falling_obj[1].y, falling_obj[1].vy);

 integrate(falling_obj);

}

SDL_Quit();

}


This program prints:
y:10.000000 vy:-98.000000
y:0.000000 vy:0.000000
y:0.000000 vy:0.000000
y:0.000000 vy:0.000000

y:0.000000 vy:0.000000
ESC!


Looks like in the while loop of the function SDL_PollEvent the variable
gets trashed, i can say so because i comment that and the program acts
normally (But obviously i can’t interact nor exit the program).

The integrate function takes the pointer to the structure’s array and
iterate over them taking a delta in time adding constant velocity. But the
vars get lost before that happens. And actually if i print the first
array’s structure (falling_obj[0]) everything is ok. The problem seems to
be only with the second structure.

My system is FreeBSD 7 running on a Intel Xeon 2.2 Dual CPU and the SDL
program running over Xorg. Also, my compile line is “gcc -I.
-I/usr/local/lib -lSDL -lGL -lGLU testSDL.c -o testSDL”, and compiles
without errors at all. Any help is appreciated, thanks in advance.

It doesn’t really look like this is the fault of any code that you have posted here. All I can see is that you have a non-conformant main signature. Change that to ‘int main(int argc, char* argv[])’. Try adding printing commands between every line in the while loop. If the program output changes, then you probably have some uninitialized variables (pointers especially) doing bad things. Also, try commenting out all other stuff to isolate the cause (i.e. comment out your integrate call). Try setting your values in a sequence (x = 1, y = 2, vx = 3…) so you can tell that the expected values are being printed.

Jonny D----------------------------------------

Date: Thu, 23 Oct 2008 18:52:22 -0300
From: rcarvajal at scv.cl
To: sdl at lists.libsdl.org
Subject: [SDL] Strage behavior in SDL_PollEvents.

In a normal while loop for processing events a specific structure gets
trashed.

Can anyone explain why this could be happening:


#include
#include
#include

#include
#include

struct particle {

 float x, vx;
 float y, vy;

};

int main() {

SDL_Event event;
int done=0;
struct particle falling_obj[2];

falling_obj[0].x=falling_obj[0].vx=0.0f;
falling_obj[0].y=10.0f;
falling_obj[0].vy=-98.0f;

falling_obj[1].x=falling_obj[1].vx=0.0f;
falling_obj[1].y=10.0f;
falling_obj[1].vy=-98.0f;

SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(800,600,24,SW_SURFACE | SW_FULLSCREEN | SW_OPENGL);

setup_opengl(); // Just the usual stuff…

while(!done) {

 fprintf(stderr, "y:%f vy:%f\n", falling_obj[1].y, falling_obj[1].vy);

 while(SDL_PollEvents(&event)) {
      if(event.type==SDL_KEYDOWN) {
           switch(event.key.keysym.sym) {
                case SDL_ESCAPE:
                fprintf(stderr, "ESC!\n\n");
                done=1;
                break;
           }
      }
 }

 fprintf(stderr, "y:%f vy:%f\n", falling_obj[1].y, falling_obj[1].vy);

 integrate(falling_obj);

}

SDL_Quit();

}


This program prints:
y:10.000000 vy:-98.000000
y:0.000000 vy:0.000000
y:0.000000 vy:0.000000
y:0.000000 vy:0.000000

y:0.000000 vy:0.000000
ESC!


Looks like in the while loop of the function SDL_PollEvent the variable
gets trashed, i can say so because i comment that and the program acts
normally (But obviously i can’t interact nor exit the program).

The integrate function takes the pointer to the structure’s array and
iterate over them taking a delta in time adding constant velocity. But the
vars get lost before that happens. And actually if i print the first
array’s structure (falling_obj[0]) everything is ok. The problem seems to
be only with the second structure.

My system is FreeBSD 7 running on a Intel Xeon 2.2 Dual CPU and the SDL
program running over Xorg. Also, my compile line is “gcc -I.
-I/usr/local/lib -lSDL -lGL -lGLU testSDL.c -o testSDL”, and compiles
without errors at all. Any help is appreciated, thanks in advance.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


When your life is on the go?take your life with you.
http://clk.atdmt.com/MRT/go/115298558/direct/01/

integrate(&falling_obj);

integrate(struct particle *falling_obj)
{
falling_obj[0]->x ?
falling_obj[1]->y ?
bla…
}On Thursday 23 October 2008 22:52:22 rcarvajal at scv.cl wrote:

integrate(falling_obj);

We need to know the code of the “integrate” function.
Your event loop seems just OK, the only possibiilty is that your data get trashed
in the functions modifying it and not SDL_PollEvent.
Also, your data seems to be global data, so give us a check list of everything likely to modify your data (including “integrate”).

Cheers

— En date de?: Ven 24.10.08, Graham Houston <graham.houston at rushpark.co.uk> a ?crit?:de: Graham Houston <graham.houston at rushpark.co.uk>
Objet: Re: [SDL] Strage behavior in SDL_PollEvents.
?: “A list for developers using the SDL library. (includes SDL-announce)”
Date: Vendredi 24 Octobre 2008, 2h58

On Thursday 23 October 2008 22:52:22 rcarvajal at scv.cl wrote:

integrate(falling_obj);

integrate(&falling_obj);

integrate(struct particle *falling_obj)
{
falling_obj[0]->x ?
falling_obj[1]->y ?
bla…
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Actually, in a gdb session i have set a watchpoint in the var falling_obj[1].y, made the program run and big is my surprise to see that the watchpoint breaks at SDL_PeepEvents … inside the libsdl.so library, telling me that the value has changed from 10 to 0. I can’t imagine why that function is messing with the other vars in my program. Also i have commented lines throught the program and the integrate function works well. Also a if that i built for draw circles corrupt a variable too. I’m really are getting a little confused.

I gonna try a different main signature but i can’t understand why that could be the cause for so much mess with other variables.

Thanks for the help.>Date: Thu, 23 Oct 2008 20:43:07 -0400

From: Jonathan Dearborn
Subject: Re: [SDL] Strage behavior in SDL_PollEvents.
To: “A list for developers using the SDL library. (includes
SDL-announce)”
Message-ID:
Content-Type: text/plain; charset=“Windows-1252”

It doesn’t really look like this is the fault of any code that you have posted here. All I can see is that you have
a non-conformant main signature. Change that to ‘int main(int argc, char* argv[])’. Try adding printing
commands between every line in the while loop. If the program output changes, then you probably have some
uninitialized variables (pointers especially) doing bad things. Also, try commenting out all other stuff to isolate
the cause (i.e. comment out your integrate call). Try setting your values in a sequence (x = 1, y = 2, vx = 3…)
so you can tell that the expected values are being printed.

Jonny D