0
\$\begingroup\$

I'm writing an SFML application in C. Currently sfRenderWindow_pollEvent() and sfRenderWindow_waitEvent() always segfault.

Here's the cleaned code that shows the issue.

sfRenderWindow* window;
sfVideoMode mode = {SCREEN_WIDTH, SCREEN_HEIGHT, 32};
window = sfRenderWindow_create(mode, "SFML window", sfResize | sfClose, NULL);

sfEvent* InputEvent;

while(1)
{
    while (sfRenderWindow_pollEvent(window, InputEvent) == sfTrue) 
    {
        ...
    }
}
\$\endgroup\$
1
\$\begingroup\$

You forgot to initialize your InputEvent pointer. Currently you only declare the pointer with sfEvent* InputEvent;, so there is actually no memory allocated for the event.

You need to either change the line to sfEvent InputEvent; and change the while-loop to while (sfRenderWindow_pollEvent(window, &InputEvent) == sfTrue), or you can manually allocate space for the event by calling InputEvent = malloc(sizeof(sfEvent));

\$\endgroup\$
  • \$\begingroup\$ Yes, this works \$\endgroup\$ – Peter Brooks Mar 12 '16 at 8:54
  • 2
    \$\begingroup\$ Please accept the answer and feel free to leave an upvote too. I'm happy I could help. \$\endgroup\$ – Tyyppi_77 Mar 12 '16 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.