0

我的 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 *

======= 回溯:=========

/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]

./app[0x4041da]

./app[0x404985]

./app[0x403322]

./app[0x4036a0]

./app[0x401dd9]

/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f78a04ee76d]

./app[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 [堆]

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(核心转储)

4

1 回答 1

1

显然SDL_FreeSurface()会捕获一个 NULL 指针,所以这不是因为您未能检查 的返回值TTF_RenderText_Solid()根据此 API 参考,它将在出错时返回 NULL。

SDL_BlitSurface()但是,我不知道这意味着什么,所以无论如何你都应该检查一下。我在BlitSurface 的一些文档中注意到

如果任何一个表面在视频内存中,并且 blit 返回 -2,则视频内存丢失,因此应该重新加载图稿并重新 blit...

这可能有一些意义。

WRT对此:

printf("Last thing that is printed\n");
SDL_FreeSurface(surface_text);
printf("Not reaching here...\n", surface_text);

尽管换行符可能会刷新 stdout 缓冲区,但最好使用 stderr -- fprintf(stderr, "...")-- 对于这种调试,因为 stderr 是无缓冲的,并且程序实际上可能会在执行第二个 printf()之后但stdout 缓冲区之前死掉被冲洗。骗人的!

于 2013-09-10T16:56:48.747 回答