SDL_PollEvent after SDL_SetVideoMode causes access violation

Hi all,

This is what happened:
i’m finally ready to compile the first private release version of my lib so i disable
CodeGuard validation and set project option to release build. What do i see,…
it crashes big time.

The problem:
I tracked the problem down to the SDL_PollEvent( &event ) and SDL_SetVideoMode combo.
If SDL_PollEvent is called after SDL_SetVideoMode the next function call or assignment after
SDL_PollEvent will cause a access violation. CodeGuard seems to prevent this somehow
because with CodeGuard enabled everything works fine. It’s not a memory corruption issue,
i have checked that. The problem does not occure either when i disable codeguard and put the
call to SDL_PollEvent in the function that calls SDL_SetVideoMode.

the code normally follows this sequence:

  • CEngineCore::Start()
    -> CEngineCore::Size_Screen() - this has SDL_SetVideoMode
    -> CEngineCore::SDL_Event_Handler() - this has SDL_PollEvent

this only works with CodeGuard enabled.
With CodeGuard disabled this works:

  • CEngineCore::Start()
    -> CEngineCore::Size_Screen() - this has SDL_SetVideoMode
    -> SDL_PollEvent

But this is not an option because i need to process events each frame offcourse.

My setup:

  • Borland C++ Builder 6
  • console application project.
  • C++
  • CodeGuard checks the following:
    -> Validate global and stack access
    -> Validate the ‘this’ pointer
    -> Validate pointer access
  • Windows XP Pro NL
  • Inspiron 4150 notebook, 512 DDR ram, Radeon 7500 mobile, 1.6Ghz P4.

I hope anyone has a clue what to do :confused:
Any suggestions/help/examples/whatever is appreciated.

GreeTz--------------------------------
| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

violation

Can you send me your code so I can take a look?

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

I created a test application (with source code offcourse) which recreates
the access violation.
It can be downloaded at the following location:
http://users.skynet.be/GalaxyUnlimited/GUCEF/SDL_Access_Violation_Test.zip

Please read the included README.txt file.

GreeTz.


| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

----- Original Message -----
From: slouken@devolution.com (Sam Lantinga)
To: “Dinand Vanvelzen” <@Dinand_Vanvelzen>
Sent: Friday, August 01, 2003 5:49 AM
Subject: Re: [SDL] SDL_PollEvent after SDL_SetVideoMode causes access

violation

Can you send me your code so I can take a look?

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

I created a test application (with source code offcourse) which recreates
the access violation.
It can be downloaded at the following location:
http://users.skynet.be/GalaxyUnlimited/GUCEF/SDL_Access_Violation_Test.zip

I tried to build it under Linux, but I didn’t see any main().
Is it a complete test application?

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment> ----- Original Message -----

From: “Sam Lantinga”
To: “Dinand Vanvelzen”
Sent: Friday, August 01, 2003 5:49 AM
Subject: Re: [SDL] SDL_PollEvent after SDL_SetVideoMode causes access

Had some trouble finding your e-mail between al my spam.
I see that I somehow missed one file when zipping it. I re-zipped and
re-uploaded.
Don’t see how I could have missed it :confused:

I don’t know if you will get the same problem on Linux, haven’t tried that.

Tnx & GreeTz.--------------------------------
| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

I created a test application (with source code offcourse) which
recreates

the access violation.
It can be downloaded at the following location:

http://users.skynet.be/GalaxyUnlimited/GUCEF/SDL_Access_Violation_Test.zip

I tried to build it under Linux, but I didn’t see any main().
Is it a complete test application?

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Had some trouble finding your e-mail between al my spam.
I see that I somehow missed one file when zipping it. I re-zipped and
re-uploaded.
Don’t see how I could have missed it :confused:

I don’t know if you will get the same problem on Linux, haven’t tried that.

Nope, it works here.
I’ll take a look on Windows when I get to work on Monday.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

One thing looked suspicious in your code, when I tried to compile it,
the linker complained that SDL_main was missing. This was because main()
was declared incorrectly, and you weren’t linking with SDLmain.lib

The attached files work fine for me with the latest versions of SDL_net
and NET2 and the CVS version of SDL. I suspect that if you just have main()
declared correctly your code won’t segfault. SDL does some DirectInput
initialization in SDLmain.lib which must happen before the rest of your
program runs.

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/octet-stream
Size: 5476 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030810/42580aac/attachment.obj

May i ask which compiler do you use ?
I tried the modified code you sent me but the problem remains.
I just created the .lib file from the dll because VC lib’s are not
compatible.
so i offcourse have to add that to my project and link to it. I use the dll
because
when I try to build SDL it always gives me compiler errors. So the lib file
I
use is SDL.lib because the dll is named SDL.dll. I use implib.exe and
impdef.exe
to create the lib file. Sometimes the’s libs don’t work when created from a
VC dll
so i have another program to fix the function definitions in the .def if
that happens.
main declared with ( void ) is perfectly legal according to my uni
professor.
he could be wrong though :slight_smile: and my compiler accepts it just fine.
I just updated my SDL_net to version 1.2.5, it was 1.2.4 but this had no
effect on the
problem.
If you do not have the problem on WIN32 with the same SDL_net and NET2
versions
then that would suggest one of two things imho. Either the compiler you use
does something different
or the CVS version of SDL has a fix for this problem. If you are using VC
then i suspect the first.
In case of the second i would need someone to compile a dll from the latest
CVS version for me
because like i said before it won’t compile here.

Anyone else out there using Borland C++ Builder 6 ?
If so could you give the test program a try plz.

Tnx & GreeTz.--------------------------------
| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

----- Original Message -----
From: slouken@devolution.com (Sam Lantinga)
To: “Dinand Vanvelzen” <@Dinand_Vanvelzen>
Cc:
Sent: Sunday, August 10, 2003 11:43 PM
Subject: Re: [SDL] SDL_PollEvent after SDL_SetVideoMode causes access
violation

One thing looked suspicious in your code, when I tried to compile it,
the linker complained that SDL_main was missing. This was because main()
was declared incorrectly, and you weren’t linking with SDLmain.lib

The attached files work fine for me with the latest versions of SDL_net
and NET2 and the CVS version of SDL. I suspect that if you just have
main()
declared correctly your code won’t segfault. SDL does some DirectInput
initialization in SDLmain.lib which must happen before the rest of your
program runs.

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

May i ask which compiler do you use ?

I’m using gcc from the Cygwin distribution.

I tried the modified code you sent me but the problem remains.

Are you linking with SDLmain.lib? That’s a critical step in the fix.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

violation

Are you linking with SDLmain.lib? That’s a critical step in the fix.

I can’t use the SDLmain.lib file in the VC package like i said in my mail
because it’s not compatible with
Borland C++ Builder 6. VC lib != BCB lib.

[Linker Error] ‘C:\GUCE\GUCE_DEV\GUCEF\SDLMAIN.LIB’ contains invalid OMF
record, type 0x21 (possibly COFF)

so i need to make one for BCB or use some other solution.


| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

----- Original Message -----
From: slouken@devolution.com (Sam Lantinga)
To:
Sent: Monday, August 11, 2003 5:53 PM
Subject: Re: [SDL] SDL_PollEvent after SDL_SetVideoMode causes access

I can’t use the SDLmain.lib file in the VC package like i said in my mail
because it’s not compatible with
Borland C++ Builder 6. VC lib != BCB lib.

[Linker Error] ‘C:\GUCE\GUCE_DEV\GUCEF\SDLMAIN.LIB’ contains invalid OMF
record, type 0x21 (possibly COFF)

so i need to make one for BCB or use some other solution.

Grab src/main/win32/SDL_main.c from the SDL source archive and include that
in your application. SDLmain.lib is just that file, compiled.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment


main declared with ( void ) is perfectly legal according to my uni
professor. he could be wrong though :slight_smile: and my compiler accepts it just
fine.


Whilst the cause of your problem has since become a little clearer - I
think this line has been overlooked. Your professor is not wrong -
void main(void) is perfectly legal C.

Unfortunately - you’re not right. Think about how SDL is
cross-platform. Now think that Linux programs start with main(…),
but Windows starts instead with Winmain(…). Because of this, SDL
hijacks your main function and does something funky with it depending
on which platform you’re compiling on.

Moral of the story - if you want to be cross platform, main needs to be
declared exactly as stated in the docs, with the 2 arguments. As Sam
said - SDL secretly does some initialisation of it’s own, like firing
up DirectInput, which you just don’t see.

Hope you get to the bottom of your problem - this was just something of
an aside for future readers.


Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software

int main(void) is legal, void main(void) is not.On Monday 11 August 2003 02:24 pm, Paul Smith wrote:

main declared with ( void ) is perfectly legal according to my uni
void main(void) is perfectly legal C.

void main(void) works on many different compilers but int main(void) is the
propper way and will work on all (AFAIK) compilers! (:> ----- Original Message -----

From: j_post@pacbell.net (Jeff Post)
To:
Sent: Monday, August 11, 2003 4:10 PM
Subject: Re: [SDL] SDL_PollEvent after SDL_SetVideoMode causes access
violation

On Monday 11 August 2003 02:24 pm, Paul Smith wrote:

main declared with ( void ) is perfectly legal according to my uni
void main(void) is perfectly legal C.

int main(void) is legal, void main(void) is not.


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

violation

void main(void) works on many different compilers but int main(void) is
the
propper way and will work on all (AFAIK) compilers! (:

int main(void) is legal, void main(void) is not.

I actually meant to type int main( void ) lol.
That’s what ya get for reading mail and coding at the same time :slight_smile:


| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

----- Original Message -----
From: atrix2@cox.net (atrix2)
To:
Sent: Tuesday, August 12, 2003 1:21 AM
Subject: Re: [SDL] SDL_PollEvent after SDL_SetVideoMode causes access

Paul Smith escribi?:


main declared with ( void ) is perfectly legal according to my uni
professor. he could be wrong though :slight_smile: and my compiler accepts it just
fine.


Whilst the cause of your problem has since become a little clearer - I
think this line has been overlooked. Your professor is not wrong -
void main(void) is perfectly legal C.

Hi

Just a comment: void main(void) its?n legal in C nor C++. In fact the
standard only allow int as return type for main. “Old” compilers seem to
forget this, but new one?s does not (i.e: gcc 3.2 and VC++ .NET) and
throw an error.

I also would like to introduce myself (I?m a newby, both in the list and
using SDL), and apology for my english, which can become terrible quite
often :frowning:

Regards.–

  Ricardo Amores Hern?ndez
ICQ: 19463735
MSN: zheo_ (at) hotmail (dot) es

violation

  • CLEANUP

Grab src/main/win32/SDL_main.c from the SDL source archive and include
that
in your application. SDLmain.lib is just that file, compiled.

I made a new project that uses winmain and stepped trough the code after
including SDL_main.c
i placed my code that used to be in main in SDL_main and it got called.
console_main ect.
all got processed before the call to SDL_main was made.
Altough all the startup SDL code is processed before my code the problem
remains.
I still get the access violation and i can still prevent it by enabling
codegaurd stack validation.

more suggestions are welcome :slight_smile:
GreeTz.


| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

----- Original Message -----
From: slouken@devolution.com (Sam Lantinga)
To:
Sent: Monday, August 11, 2003 7:50 PM
Subject: Re: [SDL] SDL_PollEvent after SDL_SetVideoMode causes access

violation

Paul Smith escribi?:

  • CLEANUP

think this line has been overlooked. Your professor is not wrong -
void main(void) is perfectly legal C.

Hi

Just a comment: void main(void) its?n legal in C nor C++. In fact the
standard only allow int as return type for main. “Old” compilers seem to
forget this, but new one?s does not (i.e: gcc 3.2 and VC++ .NET) and
throw an error.

  • CLEANUP

Just so we are clear… i meant int main( void ) and NOT void main( void ).
As far as i know void main( void ) is illegal in C and C++ and should not be
accepted
by any compiler.

GreeTz.


| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

----- Original Message -----
From: ricky@telecable.es (Ricardo Amores Hernandez)
To:
Sent: Tuesday, August 12, 2003 9:33 AM
Subject: Re: [SDL] SDL_PollEvent after SDL_SetVideoMode causes access