0

After initialize SDL with SDL_Init and IMG_Init i observe the code 11 on errno. It means that Resource temporarily unavailable. But i don't know why. Please help me resolve this problem. I observe the value of errno before and after initialisation.

#include <stdio.h>
#include <stdlib.h>
#include "../includes/init.h"
#include "../includes/map.h"
#include "../includes/startGame.h"
#include <errno.h>

int main(int argc, char const *argv[]){
int game = 1;
SDL_Window* screen = NULL;
SDL_Renderer *screen_render = NULL;
SDL_Event e;

initAllScreenAndRenderer(&screen, &screen_render);

while (game){

    while (SDL_PollEvent(&e)){
        switch (e.type){
        case SDL_QUIT:
            game=0;
            break;

        case SDL_KEYDOWN:
            switch (e.key.keysym.sym){
                case  SDLK_a:
                play(&screen_render);
                break;
            }
            break;
        }
    }
    SDL_RenderClear(screen_render);
    SDL_RenderPresent(screen_render);
}
destroyAllScreenAndRenderer(&screen, &screen_render);
}

void initAllScreenAndRenderer(SDL_Window **window, SDL_Renderer **window_renderer){
printf("\n%d\n", errno);

if (SDL_Init(SDL_INIT_VIDEO) == -1 || IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG){
    printf("\nError of initialisation : %s\n", SDL_GetError());
    exit(-1);
}
printf("\n%d\n", errno);

*window = SDL_CreateWindow(GAME_NAME, SDL_WINDOWPOS_UNDEFINED, `SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGTH, SDL_WINDOW_SHOWN);`
if (*window==NULL){
    printf("\nError of initialisation window : %s\n", SDL_GetError());
    exit(-1);
}
printf("\n%d\n", errno);

*window_renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED);
if (*window_renderer==NULL){
    printf("\nError of initialisation renderer : %s\n", SDL_GetError());
    exit(-1);
}

printf("\n%d\n", errno);
}

1 Answers1

1

If the functions haven't reported an error, you should not check errno. A function can set errno (usually as a side-effect), even though it succeeds.

It used to be the case (and probably still is the case, but I haven't checked for several years), the many of the Standard I/O functions on Solaris would set errno to ENOTTY when the output was not to a terminal, even though the function succeeded. This is wholly legitimate.

See the C standard §7.4 Errors <errno.h> §3 where it says:

The value of errno in the initial thread is zero at program startup (the initial value of errno in other threads is an indeterminate value), but is never set to zero by any library function.202) The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard.

202) Thus, a program that uses errno for error checking should set it to zero before a library function call, then inspect it before a subsequent library function call. Of course, a library function can save the value of errno on entry and then set it to zero, as long as the original value is restored if errno's value is still zero just before the return.


TL;DR — don't check errno unless a function reports failure and is documented to set errno to a meaningful value on failure.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278