调用 SDL_SetVideoMode 时出现分段错误

Posted

技术标签:

【中文标题】调用 SDL_SetVideoMode 时出现分段错误【英文标题】:Segmentation fault when calling SDL_SetVideoMode 【发布时间】:2013-12-08 17:41:08 【问题描述】:

我在确定这里出了什么问题时遇到了问题。

我是一个小组的一员,我们正在使用 SDL 制作图形。当我运行我们的 test_SDL.cc 程序时(只是为了查看所有设置是否正确),我是唯一遇到分段错误的人。

以下是相关代码:

int main(int argc, char** arg)
 
  // Initializing
  if(-1 == SDL_Init(SDL_INIT_EVERYTHING))
        return 1;

  SDL_Surface* screennullptr;
  SDL_Surface* hellonullptr;

  screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  ...

当 SDL_SetVideoMode(...) 被执行时,我得到了分段错误。有什么线索吗?

我在 Mac OS Mavericks 上运行,并使用终端进行编译

Pingul % gcc-mp-4.8 -lstdc++ -std=c++11 -Wall -Wpedantic `sdl-config --cflags --libs` test_SDL.cc

如果我的团队中的所有其他人都使用 Ubuntu 可以有所作为。感谢大家的帮助!

编辑: 崩溃报告 处理:a.out [918] 路径:/Users/USER/Documents/*/a.out 标识符:a.out 版本:0 代码类型:X86-64(本机) 父进程:zsh [297] 责任人:终端[155] 用户 ID:501

Date/Time:       2013-12-08 21:30:32.860 +0100
OS Version:      Mac OS X 10.9 (13A603)
Report Version:  11
Anonymous UUID:  EF22B2C8-6B58-F80F-184F-61D6ECE0B474


Crashed Thread:  1

Exception Type:  EXC_CRASH (SIGSEGV)
Exception Codes: 0x0000000000000000, 0x0000000000000000

Thread 0:: Dispatch queue: com.apple.main-thread
0   libSDL-1.2.0.dylib              0x000000010d59c3a9 -[SDL_QuartzWindow display] + 33
1   com.apple.AppKit                0x00007fff9460a681 __67-[NSWindow     _updateSettingsSendingScreenChangeNotificationIfNeeded:]_block_invoke + 237
2   com.apple.AppKit                0x00007fff9460a548 NSPerformWithScreenUpdatesDisabled + 65
3   com.apple.AppKit                0x00007fff9460a4f7 -[NSWindow     _updateSettingsSendingScreenChangeNotificationIfNeeded:] + 197
4   com.apple.AppKit                0x00007fff946027e1 -[NSWindow _setFrame:updateBorderViewSize:] + 914
5   com.apple.AppKit                0x00007fff94601d5e -[NSWindow _oldPlaceWindow:] + 652
6   com.apple.AppKit                0x00007fff9460118c -[NSWindow _setFrameCommon:display:stashSize:] +     1633
7   com.apple.AppKit                0x00007fff946155bc -[NSWindow setFrameOrigin:] + 369
8   com.apple.AppKit                0x00007fff9483d561 -[NSWindow center] + 278
9   libSDL-1.2.0.dylib              0x000000010d59a386 QZ_SetVideoMode + 1652
10  libSDL-1.2.0.dylib              0x000000010d5915a9 SDL_SetVideoMode + 906
11  a.out                           0x000000010d46c7d4 SDL_main + 203
12  a.out                           0x000000010d46c0bb -[SDLMain applicationDidFinishLaunching:] + 49
13  com.apple.CoreFoundation        0x00007fff935c7fcc     __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
14  com.apple.CoreFoundation        0x00007fff934bbc5d _CFXNotificationPost + 2893
15  com.apple.Foundation            0x00007fff9419d4aa -[NSNotificationCenter     postNotificationName:object:userInfo:] + 68
16  com.apple.AppKit                0x00007fff944e7b79 -[NSApplication _postDidFinishNotification] + 289
17  com.apple.AppKit                0x00007fff944e78ac -[NSApplication _sendFinishLaunchingNotification]     + 195
18  com.apple.AppKit                0x00007fff944e4796 -[NSApplication(NSAppleEventHandling)     _handleAEOpenEvent:] + 570
19  com.apple.AppKit                0x00007fff944e41eb -[NSApplication(NSAppleEventHandling)     _handleCoreEvent:withReplyEvent:] + 242
20  com.apple.Foundation            0x00007fff941bbeaa -[NSAppleEventManager     dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 294
21  com.apple.Foundation            0x00007fff941bbd1d _NSAppleEventManagerGenericHandler + 106
22  com.apple.AE                    0x00007fff8de11e1f aeDispatchAppleEvent(AEDesc const*, AEDesc*,     unsigned int, unsigned char*) + 381
23  com.apple.AE                    0x00007fff8de11c32 dispatchEventAndSendReply(AEDesc const*, AEDesc*)     + 31
24  com.apple.AE                    0x00007fff8de11b36 aeProcessAppleEvent + 315
25  com.apple.HIToolbox             0x00007fff8e0a55f1 AEProcessAppleEvent + 56
26  com.apple.AppKit                0x00007fff944e00f6 _DPSNextEvent + 1026
27  com.apple.AppKit                0x00007fff944df8db -[NSApplication     nextEventMatchingMask:untilDate:inMode:dequeue:] + 122
28  com.apple.AppKit                0x00007fff944d39cc -[NSApplication run] + 553
29  a.out                           0x000000010d46c6df main + 1305
30  libdyld.dylib                   0x00007fff936a05fd start + 1

Thread 1 Crashed:
0   libsystem_kernel.dylib          0x00007fff8a4c4e6a __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x00007fff93697f08 _pthread_wqthread + 330
2   libsystem_pthread.dylib         0x00007fff9369afb9 start_wqthread + 13

Thread 2:: Dispatch queue: com.apple.libdispatch-manager
0   libsystem_kernel.dylib          0x00007fff8a4c5662 kevent64 + 10
1   libdispatch.dylib               0x00007fff8e52343d _dispatch_mgr_invoke + 239
2   libdispatch.dylib               0x00007fff8e523152 _dispatch_mgr_thread + 52

Thread 3:
0   libsystem_kernel.dylib          0x00007fff8a4c4e6a __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x00007fff93697f08 _pthread_wqthread + 330
2   libsystem_pthread.dylib         0x00007fff9369afb9 start_wqthread + 13

Thread 4:
0   libsystem_kernel.dylib          0x00007fff8a4c4e6a __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x00007fff93697f08 _pthread_wqthread + 330
2   libsystem_pthread.dylib         0x00007fff9369afb9 start_wqthread + 13

Thread 5:
0   libsystem_kernel.dylib          0x00007fff8a4c4e6a __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x00007fff93697f08 _pthread_wqthread + 330
2   libsystem_pthread.dylib         0x00007fff9369afb9 start_wqthread + 13

Thread 6:
0   libsystem_kernel.dylib          0x00007fff8a4c4e6a __workq_kernreturn + 10
1   libsystem_pthread.dylib         0x00007fff93697f08 _pthread_wqthread + 330
2   libsystem_pthread.dylib         0x00007fff9369afb9 start_wqthread + 13

Thread 1 crashed with X86 Thread State (64-bit):
  rax: 0x0000000002000170  rbx: 0x000000010e081000  rcx: 0x000000010e080f28  rdx: 0x0000000000000000
  rdi: 0x0000000000000004  rsi: 0x0000000000000000  rbp: 0x000000010e080f50  rsp: 0x000000010e080f28
   r8: 0x00007fff922df8d7   r9: 0x0000000000000164  r10: 0x0000000000000000  r11: 0x0000000000000246
  r12: 0x0000000000000000  r13: 0x0000000000000000  r14: 0x0000000000070001  r15: 0x0000000000001003
  rip: 0x00007fff8a4c4e6a  rfl: 0x0000000000000246  cr2: 0x000000010f352000

Logical CPU:     0
Error Code:      0x02000170
Trap Number:     133

还有

VM Region Summary:
ReadOnly portion of Libraries: Total=155.7M resident=54.7M(35%) swapped_out_or_unallocated=101.0M(65%)
Writable regions: Total=71.8M written=2836K(4%) resident=3764K(5%) swapped_out=0K(0%) unallocated=68.1M(    95%)

REGION TYPE                      VIRTUAL
===========                      =======
CG image                              4K
CG shared images                    212K
Dispatch continuations             8192K
Kernel Alloc Once                     8K
MALLOC                             36.6M
MALLOC (admin)                       32K
Memory Tag 242                       12K
STACK GUARD                        56.0M
Stack                              10.6M
VM_ALLOCATE                        16.3M
__DATA                             19.3M
__IMAGE                             528K
__LINKEDIT                         66.1M
__TEXT                             89.6M
__UNICODE                           544K
mapped file                        28.2M
shared memory                         4K
===========                      =======
TOTAL                             332.2M

Model: MacBookAir4,1, BootROM MBA41.0077.B0F, 2 processors, Intel Core i5, 1.6 GHz, 4 GB, SMC 1.74f4
Graphics: Intel HD Graphics 3000, Intel HD Graphics 3000, Built-In, 384 MB
Memory Module: BANK 0/DIMM0, 2 GB, DDR3, 1333 MHz, 0x80AD, 0x484D54333235533642465238432D48392020
Memory Module: BANK 1/DIMM0, 2 GB, DDR3, 1333 MHz, 0x80AD, 0x484D54333235533642465238432D48392020
AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0xE9), Broadcom BCM43xx 1.0 (5.106.98.100    .22)
Bluetooth: Version 4.2.0f6 12982, 3 services, 23 devices, 1 incoming serial ports
Network Service: Wi-Fi, AirPort, en0
Serial ATA Device: APPLE SSD TS128C, 121,33 GB
USB Device: FaceTime Camera (Built-in)
USB Device: Hub
USB Device: Apple Internal Keyboard / Trackpad
USB Device: BRCM20702 Hub
USB Device: Bluetooth USB Host Controller
USB Device: Hub
Thunderbolt Bus: MacBook Air, Apple Inc., 8.1

我希望这是相关信息。请告诉我我是否也在格式化这个奇怪的东西。

【问题讨论】:

您/您的小组成员使用什么版本的 sdl? @itwasntpete 我们使用的是 1.2.15。根据您的建议,我尝试将 32 更改为 24,并查看所有可用选项。唯一有效的是 SDL_FULLSCREEN,所有其他也导致了分段错误。你知道为什么吗?如果不是,我将使用我所拥有的,但否则也会​​很有趣:) 你试过SDL_GetVideoInfo() 看看什么是“可能”最好的吗?另外,请发布错误的痕迹。 在跟踪中看到的 SDL_main 仍然被调用。你在某处未定义的 sdl main 吗? undef main 代码中的某处?或尝试将您的主要内容设为 SDL_main。喜欢看here 我不知道,抱歉。你试过编译他的例子吗?还尝试使用 sdl 2 进行编译。查看跟踪日志libSDL-1.2.0,您的 sdl 版本似乎是 1.2.0。 【参考方案1】:

修复每像素位数并不是一个好主意。也不是每个 Mac-OS 都支持 SDL 上的 32 位。尝试一些其他值。 0 用于自动分配,通常不在 Mac 上运行。我认为 24​​ 应该这样做。

screen = SDL_SetVideoMode(640, 480, 24, SDL_SWSURFACE);

您也可以使用SDL_GetVideoInfo(); 来确定您应该使用哪种模式。

【讨论】:

以上是关于调用 SDL_SetVideoMode 时出现分段错误的主要内容,如果未能解决你的问题,请参考以下文章

调用 c 函数时出现分段错误

在调用函数中取消引用指针时出现分段错误

从 C 调用汇编函数时出现分段错误错误

设置堆栈后在 C 中调用 printf 时出现分段错误

调用printf%s时出现分段错误

调用0x16时出现分段错误