简体   繁体   English

初始化 SDL 后出现 errno 11

[英]errno 11 after initialize SDL

After initialize SDL with SDL_Init and IMG_Init i observe the code 11 on errno.使用 SDL_Init 和 IMG_Init 初始化 SDL 后,我在 errno 上观察到代码 11。 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.我观察了初始化前后 errno 的值。

#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);
}

If the functions haven't reported an error, you should not check errno .如果函数没有报告错误,则不应检查errno A function can set errno (usually as a side-effect), even though it succeeds. function 可以设置errno (通常作为副作用),即使它成功了。

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.它曾经是这种情况(可能仍然这种情况,但我已经好几年没有检查过了),当 output 不是终端时,Solaris 上的许多标准 I/O 函数会将errnoENOTTY ,即使 function 成功了。 This is wholly legitimate.这是完全合法的。

See the C standard §7.4 Errors <errno.h> §3 where it says:请参阅 C 标准§7.4 Errors <errno.h> §3 ,它说:

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.初始线程中errno的值在程序启动时为零(其他线程中errno的初始值为不确定值),但永远不会被任何库设置为零 function。202 errno的值可能被设置为非零由库 function 调用是否有错误,前提是errno的使用未记录在本国际标准中 function 的描述中。

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. 202)因此,使用errno进行错误检查的程序应该在库 function 调用之前将其设置为零,然后在后续库 function 调用之前检查它。 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.当然,库 function 可以在进入时保存errno的值,然后将其设置为零,只要在返回之前errno的值仍然为零,只要恢复原始值即可。


TL;DR — don't check errno unless a function reports failure and is documented to set errno to a meaningful value on failure. TL;DR — 不要检查errno ,除非 function 报告失败并且记录在失败时将errno为有意义的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM