我的SDL应用程序偶尔崩溃,出现了一个非常奇怪的错误。
glibc抱怨双重免费或腐败"错误并导致程序崩溃。我没有任何多线程,所讨论的代码只释放已在同一静态函数中分配的表面。
执行对象转储,我发现崩溃发生在以下代码段中。
代码段:
static void draw_text_to_screen(const char *text, TTF_Font *text_font,
SDL_Color text_color, unsigned int x_offset, unsigned int y_offset)
{
SDL_Rect offset = (SDL_Rect) {x_offset, y_offset, 0, 0};
SDL_Surface *surface_text =
TTF_RenderText_Solid(text_font, text, text_color);
SDL_BlitSurface(surface_text, NULL, screen, &offset);
printf("Last thing that is printed\n");
SDL_FreeSurface(surface_text);
printf("Not reaching here...\n", surface_text);
}
*** glibc检测到*** ./app:双重免费或损坏(输出):0x0000000002bf82d0 *
======= Backtrace:=========
/lib/x86_64-linux-gnu/libc.so.6(+ 0x7eb96)[0x7f78a054bb96]
/usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0(SDL_FreeSurface + 0xdd)[0x7f78a0cdf9dd]
./应用[0x4041da]
./应用[0x404985]
./应用[0x403322]
./应用[0x4036a0]
./应用[0x401dd9]
/lib/x86_64-linux-gnu/libc.so.6(__ libc_start_main + 0xed)[0x7f78a04ee76d]
./应用[0x401f25]
=======记忆图:========
00400000-0040c000 r-xp 00000000 08:05 3805386 / home / me / repos / audio / app
0060b000-0060c000 r - p 0000b000 08:05 3805386 / home / me / repos / audio / app
0060c000-0060d000 rw-p 0000c000 08:05 3805386 / home / me / repos / audio / app
02199000-03307000 rw-p 00000000 00:00 0 [heap]
7f7899461000-7f7899476000 r-xp 00000000 08:05 3804734 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f7899476000-7f7899675000 --- p 00015000 08:05 3804734 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f7899675000-7f7899676000 r - p 00014000 08:05 3804734 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f7899676000-7f7899677000 rw-p 00015000 08:05 3804734 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f7899677000-7f7899ace000 rw -s 00000000 00:04 229377 / SYSV00000000(已删除)
7f7899ace000-7f7899ad3000 r-xp 00000000 08:05 4857142 /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
7f7899ad3000-7f7899cd2000 --- p 00005000 08:05 4857142 /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
7f7899cd2000-7f7899cd3000 r - p 00004000 08:05 4857142 /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
7f7899cd3000-7f7899cd4000 rw-p 00005000 08:05 4857142 /usr/lib/x86_64-linux-gnu/libXfixes.so.3.1.0
7f7899cd4000-7f7899cdd000 r-xp 00000000 08:05 4857160 /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
7f7899cdd000-7f7899edc000 --- p 00009000 08:05 4857160 /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
7f7899edc000-7f7899edd000 r - p 00008000 08:05 4857160 /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
7f7899edd000-7f7899ede000 rw-p 00009000 08:05 4857160 /usr/lib/x86_64-linux-gnu/libXrender.so.1.3.0
7f7899ede000-7f7899ee7000 r-xp 00000000 08:05 4857134 /usr/lib/x86_64-linux-gnu/libXcursor.so.1.0.2
7f7899ee7000-7f789a0e6000 --- p 00009000 08:05 4857134 /usr/lib/x86_64-linux-gnu/libXcursor.so.1.0.2
7f789a0e6000-7f789a0e7000 r - p 00008000 08:05 4857134 /usr/lib/x86_64-linux-gnu/libXcursor.so.1.0.2
7f789a0e7000-7f789a0e8000 rw-p 00009000 08:05 4857134 /usr/lib/x86_64-linux-gnu/libXcursor.so.1.0.2
7f789a0e8000-7f789a7cb000 r - p 00000000 08:05 4856463 / usr / lib / locale / locale-archive
7f789a7cb000-7f789a7cc000 --- p 00000000 00:00 0
7f789a7cc000-7f789afcc000 rw-p 00000000 00:00 0
7f789afcc000-7f789afe4000 r-xp 00000000 08:05 3801993 /lib/x86_64-linux-gnu/libresolv-2.15.so
7f789afe4000-7f789b1e4000 --- p 00018000 08:05 3801993 /lib/x86_64-linux-gnu/libresolv-2.15.so
7f789b1e4000-7f789b1e5000 r - p 00018000 08:05 3801993 /lib/x86_64-linux-gnu/libresolv-2.15.so
7f789b1e5000-7f789b1e6000 rw-p 00019000 08:05 3801993 /lib/x86_64-linux-gnu/libresolv-2.15.so
7f789b1e6000-7f789b1e8000 rw-p 00000000 00:00 0
7f789b1e8000-7f789b1ee000 r-xp 00000000 08:05 4857537 /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7f789b1ee000-7f789b3ed000 --- p 00006000 08:05 4857537 /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7f789b3ed000-7f789b3ee000 r - p 00005000 08:05 4857537 /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7f789b3ee000-7f789b3ef000 rw-p 00006000 08:05 4857537 /usr/lib/x86_64-linux-gnu/libogg.so.0.7.1
7f789b3ef000-7f789b41a000 r-xp 00000000 08:05 4857695 /usr/lib/x86_64-linux-gnu/libvorbis.so.0.4.5
7f789b41a000-7f789b619000 --- p 0002b000 08:05 4857695 /usr/lib/x86_64-linux-gnu/libvorbis.so.0.4.5
7f789b619000-7f789b61a000 r - p 0002a000 08:05 4857695 /usr/lib/x86_64-linux-gnu/libvorbis.so.0.4.5
7f789b61a000-7f789b61b000 rw-p 0002b000 08:05 4857695 /usr/lib/x86_64-linux-gnu/libvorbis.so.0.4.5
7f789b61b000-7f789b8ce000 r-xp 00000000 08:05 4857697 /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2.0.8
7f789b8ce000-7f789bacd000 --- p 002b3000 08:05 4857697 /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2.0.8
7f789bacd000-7f789bae9000 r - p 002b2000 08:05 4857697 /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2.0.8
7f789bae9000-7f789baea000 rw-p 002ce000 08:05 4857697 /usr/lib/x86_64-linux-gnu/libvorbisenc.so.2.0.8
7f789baea000-7f789bb32000 r-xp 00000000 08:05 4857070 /usr/lib/x86_64-linux-gnu/libFLAC.so.8.2.0
7f789bb32000-7f789bd32000 --- p 00048000 08:05 4857070 /usr/lib/x86_64-linux-gnu/libFLAC.so.8.2.0
7f789bd32000-7f789bd33000 r - p 00048000 08:05 4857070 /usr/lib/x86_64-linux-gnu/libFLAC.so.8.2.0
7f789bd33000-7f789bd34000 rw-p 00049000 08:05 4857070 /usr/lib/x86_64-linux-gnu/libFLAC.so.8.2.0
7f789bd34000-7f789bd4b000 r-xp 00000000 08:05 3804922 /lib/x86_64-linux-gnu/libnsl-2.15.so
7f789bd4b000-7f789bf4a000 --- p 00017000 08:05 3804922 /lib/x86_64-linux-gnu/libnsl-2.15.so
7f789bf4a000-7f789bf4b000 r - p 00016000 08:05 3804922 /lib/x86_64-linux-gnu/libnsl-2.15.so
7f789bf4b000-7f789bf4c000 rw-p 00017000 08:05 3804922 /lib/x86_64-linux-gnu/libnsl-2.15.so
7f789bf4c000-7f789bf4e000 rw-p 00000000 00:00 0
7f789bf4e000-7f789bf53000 r-xp 00000000 08:05 4857138 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
7f789bf53000-7f789c152000 --- p 00005000 08:05 4857138 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
7f789c152000-7f789c153000 r - p 00004000 08:05 4857138 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
7f789c153000-7f789c154000 rw-p 00005000 08:05 4857138 /usr/lib/x86_64-linux-gnu/libXdmcp.so.6.0.0
7f789c154000-7f789c156000 r-xp 00000000 08:05 4857127 /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0
7f789c156000-7f789c355000 --- p 00002000 08:05 4857127 /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0
7f789c355000-7f789c356000 r - p 00001000 08:05 4857127 /usr/lib/x86_64-linux-gnu/libXau.so.6.0.0Aborted(core dumped)
答案 0 :(得分:1)
显然SDL_FreeSurface()
会捕获一个NULL指针,所以这不是因为你没有检查TTF_RenderText_Solid()
的返回值,according to this API reference将在出错时返回NULL。
但是,这对我不知道的SDL_BlitSurface()
意味着什么,所以无论如何你应该检查一下。我在some documentation for BlitSurface注意到:
如果其中一个曲面位于视频内存中,并且blit返回-2,则视频内存丢失,因此应该重新加载艺术作品并重新显示...
这可能有一些意义。
WRT to this:
printf("Last thing that is printed\n");
SDL_FreeSurface(surface_text);
printf("Not reaching here...\n", surface_text);
虽然换行符可能会刷新stdout缓冲区,但使用stderr - fprintf(stderr, "...")
- 更好的做法是进行这种调试,因为stderr unbuffered 并且程序可能实际上 之后执行第二次printf(),但之前刷新stdout缓冲区。欺骗性!