为什么SDL_WaitEvent(SDL2)函数在我的代码中出错?

问题描述 投票:0回答:1

我正在研究狼,有时收到错误,发现它是段错误,有时因为sig中止而出错,有时我们也遇到此错误:'线程已经在初始化该类!'

我使用了lldb,他表明功能WaitEvent出错。 SDL_Event事件;是通过此功能初始化的,有些我不了解如何得到错误。

int     main(int ac, char **av)
{

    int             fd;
    t_var           var;
    t_player        player;

    ft_init_sdl(&var);
    init_key_move(&var); // int attribution
    if (ac != 2 || (fd = open(av[1], O_RDONLY)) < 0)
        exit(EXIT_FAILURE);
    parsing_map(fd, &var); // map parsing 
    var.m = fill_map_struct(var); // map parsing again
    player_data_set(&player, &var); // int atribution for the player
    open_wall_texture(&var); //loading texture form bmp with SDL_LoadBPM
    open_img_opt_button(&var); // same
    display(&var, &player); // displaying the game
    return (0);
}

    void    ft_init_sdl(t_var *var)
    {
        var->sdl.window = NULL;
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0)
            return ;
        var->sdl.window = SDL_CreateWindow("WOLFCRAFT", SDL_WINDOWPOS_CENTERED, \
                SDL_WINDOWPOS_CENTERED, SIZE_X, SIZE_Y, 0);
        var->sdl.render = SDL_CreateRenderer(var->sdl.window, -1, 0);
        if (!var->sdl.window || !var->sdl.render)
            return ;
    }

void    display_3(t_var *var, t_player *pl)
{
    if (var->sdl.event.type == SDL_QUIT)
        ft_clean_quit(var->sdl.render, var->sdl.window);
    else if (var->sdl.event.key.keysym.sym == SDLK_ESCAPE)
        ft_clean_quit(var->sdl.render, var->sdl.window);
    else if (var->sdl.event.type == SDL_KEYDOWN && var->menu_is_act == 1)
        ft_move(var->sdl.event.key.keysym.sym, pl, var->sdl.render, var);
    if (var->sdl.event.type == SDL_MOUSEMOTION && (var->menu_is_act == 1))
    {
        var->on = 1;
        menu_button_in_game(var, pl);
    }
    else if (var->sdl.event.type == SDL_MOUSEBUTTONDOWN \
                    && (var->menu_is_act == 1))
    {
        var->on = 0;
        menu_button_in_game(var, pl);
    }
}

void    display_2(t_var *var, t_player *pl, int *token)
{
    init_var_display(var, token);
    ft_menu(var, pl);
}

void    display(t_var *var, t_player *pl)
{
    int token;

    display_2(var, pl, &token);
    while (SDL_WaitEvent(&var->sdl.event))
    {
        if (var->sdl.event.type == SDL_MOUSEMOTION \
                && (var->menu_is_act == 0 || var->menu_is_act == 2))
        {
            var->on = 1;
            ft_menu(var, pl);
        }
        else if (var->sdl.event.type == SDL_MOUSEBUTTONDOWN \
                        && (var->menu_is_act == 0 || var->menu_is_act == 2))
        {
            var->on = 0;
            ft_menu(var, pl);
        }
        else
            display_3(var, pl);
        if (var->menu_is_act == 2)
        {
            change_key(var, pl);
            choose_key(var, pl);
        }
    }
}

已经过了一个星期,我们正在寻找找不到任何东西的解决方案。

谢谢。

这是lldb用“ bt”显示给我的内容。

Process 53017 launched: '/Users/vgauther/Desktop/work/wolf3d' (x86_64)
Process 53017 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x00007fff542b22ce libobjc.A.dylib`_class_initialize + 37
libobjc.A.dylib`_class_initialize:
->  0x7fff542b22ce <+37>: movq   0x20(%r14), %rax
    0x7fff542b22d2 <+41>: movq   %rax, %rcx
    0x7fff542b22d5 <+44>: andq   %rbx, %rcx
    0x7fff542b22d8 <+47>: movq   0x8(%rcx), %rcx
Target 0: (wolf3d) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
  * frame #0: 0x00007fff542b22ce libobjc.A.dylib`_class_initialize + 37
    frame #1: 0x00007fff542b1e8f libobjc.A.dylib`lookUpImpOrForward + 228
    frame #2: 0x00007fff542b1914 libobjc.A.dylib`_objc_msgSend_uncached + 68
    frame #3: 0x00007fff2a5696b6 AppKit`-[NSTextFieldCell _allowsVibrancyForControlView:] + 394
    frame #4: 0x00007fff2a557871 AppKit`-[NSControl allowsVibrancy] + 49
    frame #5: 0x00007fff2a55782a AppKit`-[NSView(NSInternal) _needsVibrancy] + 83
    frame #6: 0x00007fff2a55766d AppKit`-[NSView(NSInternal) _updateVibrancy] + 26
    frame #7: 0x00007fff2a674a25 AppKit`-[NSView viewWillDraw] + 87
    frame #8: 0x00007fff2adeb6cf AppKit`_NSViewWillDraw + 45
    frame #9: 0x00007fff2a674e21 AppKit`-[NSView viewWillDraw] + 1107
    frame #10: 0x00007fff2adeb6cf AppKit`_NSViewWillDraw + 45
    frame #11: 0x00007fff2a674e21 AppKit`-[NSView viewWillDraw] + 1107
    frame #12: 0x00007fff2adeb6cf AppKit`_NSViewWillDraw + 45
    frame #13: 0x00007fff2a674e21 AppKit`-[NSView viewWillDraw] + 1107
    frame #14: 0x00007fff2adeb6cf AppKit`_NSViewWillDraw + 45
    frame #15: 0x00007fff2a6746b9 AppKit`-[NSView _sendViewWillDrawInRect:clipRootView:] + 1640
    frame #16: 0x00007fff2a672efd AppKit`-[NSView displayIfNeeded] + 1142
    frame #17: 0x00007fff2a672a52 AppKit`-[NSWindow displayIfNeeded] + 321
    frame #18: 0x00007fff2ae1360d AppKit`___NSWindowGetDisplayCycleObserver_block_invoke.5902 + 308
    frame #19: 0x00007fff2a67255e AppKit`__37+[NSDisplayCycle currentDisplayCycle]_block_invoke + 695
    frame #20: 0x00007fff38314877 QuartzCore`CA::Transaction::run_commit_handlers(CATransactionPhase) + 49
    frame #21: 0x00007fff38313339 QuartzCore`CA::Transaction::commit() + 171
    frame #22: 0x00007fff2ae2aa72 AppKit`__65+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayRefresh]_block_invoke + 283
    frame #23: 0x00007fff2cfb8257 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    frame #24: 0x00007fff2cfb817f CoreFoundation`__CFRunLoopDoObservers + 527
    frame #25: 0x00007fff2cf9a6f8 CoreFoundation`__CFRunLoopRun + 1240
    frame #26: 0x00007fff2cf99f93 CoreFoundation`CFRunLoopRunSpecific + 483
    frame #27: 0x00007fff2c284d96 HIToolbox`RunCurrentEventLoopInMode + 286
    frame #28: 0x00007fff2c284a0f HIToolbox`ReceiveNextEventCommon + 366
    frame #29: 0x00007fff2c284884 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 64
    frame #30: 0x00007fff2a533a3b AppKit`_DPSNextEvent + 2085
    frame #31: 0x00007fff2acc9e34 AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
    frame #32: 0x000000010017843a libSDL2-2.0.0.dylib`Cocoa_PumpEvents(_this=<unavailable>) at SDL_cocoaevents.m:438 [opt]
    frame #33: 0x00000001000d5ecd libSDL2-2.0.0.dylib`SDL_WaitEventTimeout_REAL [inlined] SDL_PumpEvents_REAL at SDL_events.c:645 [opt]
    frame #34: 0x00000001000d5eba libSDL2-2.0.0.dylib`SDL_WaitEventTimeout_REAL(event=0x00007ffeefbff1c8, timeout=-1) at SDL_events.c:680 [opt]
    frame #35: 0x00000001000030e1 wolf3d`display + 49
    frame #36: 0x00000001000012b9 wolf3d`main + 265
    frame #37: 0x00007fff54ed8015 libdyld.dylib`start + 1
/Users/vgauther/Desktop/check_compil/SDL2-2.0.9/src/video/cocoa/SDL_cocoamodes.m:118:11: warning: 'CGDisplayModeCopyPixelEncoding' is deprecated: first deprecated in macOS 10.11 [-Wdeprecated-declarations]
    fmt = CGDisplayModeCopyPixelEncoding(vidmode);
          ^
/System/Library/Frameworks/CoreGraphics.framework/Headers/CGDirectDisplay.h:176:34: note: 'CGDisplayModeCopyPixelEncoding' has been explicitly marked deprecated here
CG_EXTERN CFStringRef __nullable CGDisplayModeCopyPixelEncoding(
                                 ^
/Users/vgauther/Desktop/check_compil/SDL2-2.0.9/src/video/cocoa/SDL_cocoamodes.m:172:64: warning: 'CGDisplayIOServicePort' is deprecated: first deprecated in macOS 10.9 [-Wdeprecated-declarations]
    CFDictionaryRef deviceInfo = IODisplayCreateInfoDictionary(CGDisplayIOServicePort(displayID), kIODisplayOnlyPreferredName);
                                                               ^
/System/Library/Frameworks/CoreGraphics.framework/Headers/CGDisplayConfiguration.h:372:24: note: 'CGDisplayIOServicePort' has been explicitly marked deprecated here
CG_EXTERN io_service_t CGDisplayIOServicePort(CGDirectDisplayID display)
                       ^
2 warnings generated.
c events sdl sdl-2
1个回答
0
投票

main中,您不检查ft_sdl_init的结果是否正确,因此,即使窗口/渲染器无效,程序也可能运行。

此条件绝对不执行任何操作:

        if (!var->sdl.window || !var->sdl.render)
            return ;

如果为真,则保留该功能。如果为假,它也会离开该函数。没什么区别。

此外,如果创建窗口/渲染器失败,SDL将返回NULL。从SDL wiki on SDL_CreateWindow

返回创建的窗口,失败则返回NULL;调用SDL_GetError()以获得更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.