PollEvent() crashes app when it's passed a pointer directly

If I do this:

SDL_Event* event_taken;

while( SDL_PollEvent(event_taken) ){
switch(event_taken->type) {
case SDL_KEYDOWN:
exit(0);
break;
}
}

The compiler does not complain but the app crashes after 2-3 seconds.
(This application needs to close etc.)

If I do it as it’s in the docs, that is by creating a structure and
passing its address to PollEvent() it works.

I’m using Dev-C++, on WinXP.

its because you declare a pointer, but not any memory to store the data in!

so, when pollevent writes the data to where you told it to, that could be
anywhere in memory and it must take that 2-3 seconds before it over-writes
anything important.

try this (although you might as well just not use a pointer):

SDL_Event* event_taken;

event_taken = new SDL_Event;

while( SDL_PollEvent(event_taken) )
{
switch(event_taken->type)
{
case SDL_KEYDOWN:
{
delete event_taken;
exit(0);
break;
}
}

delete event_taken;

Hope this helps, if im not totaly missing something obvious here, you might
want to check out some tutorials on pointers and dynamic memory to help
understand whats going on (:> ----- Original Message -----

From: psymaster@caths.co.uk ()
To:
Sent: Friday, January 09, 2004 9:28 AM
Subject: [SDL] PollEvent() crashes app when it’s passed a pointer directly.

If I do this:

SDL_Event* event_taken;

while( SDL_PollEvent(event_taken) ){
switch(event_taken->type) {
case SDL_KEYDOWN:
exit(0);
break;
}
}

The compiler does not complain but the app crashes after 2-3 seconds.
(This application needs to close etc.)

If I do it as it’s in the docs, that is by creating a structure and
passing its address to PollEvent() it works.

I’m using Dev-C++, on WinXP.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Alan Wolfe wrote:

try this (although you might as well just not use a pointer):

SDL_Event* event_taken;

event_taken = new SDL_Event;

while( SDL_PollEvent(event_taken) )
{
switch(event_taken->type)
{
case SDL_KEYDOWN:
{
delete event_taken;
exit(0);
break;
}
}

delete event_taken;

Hope this helps, if im not totaly missing something obvious here, …

You are :wink:
SDL_PollEvent doesn’t create a new structure (as you noted yourself), so
you shouldn’t delete it on a KEYDOWN event… :wink:

I agree with you that it makes much more sense to allocate the event
statically, like in the SDL docs.

hey Sebastion, i put the delete in because if you notice, on key down the
program exits, we dont want memory leaks do we? (:> ----- Original Message -----

From: s.beschke@gmx.de (Sebastian Beschke)
To:
Sent: Friday, January 09, 2004 11:08 AM
Subject: Re: [SDL] PollEvent() crashes app when it’s passed a pointer
directly.

Alan Wolfe wrote:

try this (although you might as well just not use a pointer):

SDL_Event* event_taken;

event_taken = new SDL_Event;

while( SDL_PollEvent(event_taken) )
{
switch(event_taken->type)
{
case SDL_KEYDOWN:
{
delete event_taken;
exit(0);
break;
}
}

delete event_taken;

Hope this helps, if im not totaly missing something obvious here, …

You are :wink:
SDL_PollEvent doesn’t create a new structure (as you noted yourself), so
you shouldn’t delete it on a KEYDOWN event… :wink:

I agree with you that it makes much more sense to allocate the event
statically, like in the SDL docs.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Alan Wolfe wrote:

hey Sebastion, i put the delete in because if you notice, on key down the
program exits, we dont want memory leaks do we? (:

Oh…
I should’ve read it more properly… my bad ^^
Might be because I never exit from the middle of the program.

Sebastian

Alan Wolfe wrote:

hey Sebastion, i put the delete in because if you notice, on key down the
program exits, we dont want memory leaks do we? (:

Unless you’re using Windows 95 or the like a process can’t leak memory
beyond exiting. I guess you know that and that’s why you put a smiley
at the end, though. ;-)–
Christian
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040109/ef9fdbf5/attachment.pgp

no, didnt know that, thats very cool (:> ----- Original Message -----

From: christianbiere@gmx.de (Christian Biere)
To:
Sent: Friday, January 09, 2004 12:25 PM
Subject: Re: [SDL] PollEvent() crashes app when it’s passed a pointer
directly.