Author Topic: FPC and SDL2 Why does this crash  (Read 1284 times)


  • New member
  • *
  • Posts: 6
FPC and SDL2 Why does this crash
« on: December 31, 2018, 02:42:19 am »
Hi Everyone,

I have been trying to self-teach myself how to develop a game and I am using Free Pascal and SDL2 2.0.9 to learn.  I am trying to get my project to work in Ubuntu Linux 18 but it crashes with a runtime error 200 whenever I start it up.  The project works just fine on Windows 10.

I've stripped the code down to just the barebone initialization of SDL2 and it still crashes in Linux.  I then made the same code in C on the same Linux, and for some reason the identical C code works just fine but the Pascal version crashes.

I don't know if I am doing something wrong or if somehow maybe there is a problem with Free Pascal but I am at a loss as to how to resolve the crash.  I will paste the code below.  The headers I am using can be found at the following link:

I looked at the headers and I didn't see anything wrong but I am mostly a beginner so maybe the headers are not accurate or I am not using a compiler options or something?  The code itself does not seem to return any errors.

Thank you to anyone who is willing to help me figure this out!  This is the Pascal code (crashes) while C version doesn't:

Code: Pascal  [Select]
  1. Program testsdl;
  3. Uses
  4.         SDL2 in 'SDL2/sdl2.pas';
  6. Var
  7.         Window   : PSDL_Window;
  8.         Renderer : PSDL_Renderer;
  9. Begin
  10.         If SDL_Init(SDL_INIT_VIDEO) < 0 Then Begin
  11.                 WriteLn ('Error init video');
  12.                 Exit;
  13.         End;
  15.         Window := SDL_CreateWindow('SDL_RenderClear',
  17.                 640, 480,
  18.                 SDL_WINDOW_FULLSCREEN_DESKTOP);
  20.         If Window = NIL Then Begin
  21.                 WriteLn('Error creating window');
  22.                 Halt;
  23.         End;
  25.         Renderer := SDL_CreateRenderer(Window, -1, 0);
  27.         If Renderer = NIL Then Begin
  28.                 WriteLn('Error creating renderer');
  29.                 Halt;
  30.         End;
  32.         SDL_RenderClear(Renderer);
  33.         SDL_RenderPresent(Renderer);
  35.         SDL_Delay(5000);
  37.         SDL_Quit();
  38. End.


  • Newbie
  • Posts: 3
  • ookami
    • Website
Re: FPC and SDL2 Why does this crash
« Reply #1 on: December 31, 2018, 01:58:06 pm »
I remember having a similar problem on Linux with SDL2. Try adding this as the first line in your uses clause:

{$IFDEF UNIX}cthreads,{$ENDIF}


  • New member
  • *
  • Posts: 6
Re: FPC and SDL2 Why does this crash
« Reply #2 on: January 04, 2019, 12:34:39 am »
Thank you for the response.  I have tried to add the CThreads into the program but it still seems to crash in the same way.  I managed to narrow it down to the SDL_RenderClear function.  If I comment that out everything else seems to run fine and of course the C version works with the RenderClear.

I've since tried to add in some code to print out the available drivers and that part also crashes but with a different 216 error.

I just cannot seem to get it to work outside of C.


  • Hero Member
  • *****
  • Posts: 8075
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: FPC and SDL2 Why does this crash
« Reply #3 on: January 04, 2019, 01:21:10 am »
I stumble upon this thread and I tried one of the solution:
Code: [Select]
$ SDL_RENDER_DRIVER=opengl ./testsdl
Seems working.


  • New member
  • *
  • Posts: 6
Re: FPC and SDL2 Why does this crash
« Reply #4 on: February 04, 2019, 09:33:46 pm »
I've still not been able to get this to work.  I tried the openGL thing but that didn't seem to help.  I do not feel like its a driver problem because the identical code in C compiled with gcc works just fine.  The crash only happens in Free Pascal.


  • Full Member
  • ***
  • Posts: 198
Re: FPC and SDL2 Why does this crash
« Reply #5 on: February 05, 2019, 11:30:36 am »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
People call me crazy, because i'm jumping out of perfectly fine aircrafts


  • Hero Member
  • *****
  • Posts: 503
Re: FPC and SDL2 Why does this crash
« Reply #6 on: February 05, 2019, 05:15:27 pm »
Are you on 32-bit? I believe this is a known issue with alignment on 32-bit Linux, that has a known fix you can apply locally to your FPC sources if you want.


Code: [Select]
find the:

Code: [Select]
constant record definition, and under that where it currently says:

Code: [Select]
stackalign   : 4;
change it to:

Code: [Select]
stackalign   : 16;
and then rebuild the compiler / RTL / packages / e.t.c.

I believe the only reason this change has not been made in trunk is backwards-compatibility concerns (and whether that's the right decision overall is debatable I guess but a separate issue from solving your actual problem in a practical sense here.)
« Last Edit: February 05, 2019, 05:32:45 pm by Akira1364 »