realloc 在以前稳定的函数中崩溃
显然,SDL_Mixer 中的这个函数一直在消失,我不知道为什么。 有人有什么想法吗? 根据 Visual Studio 的说法,崩溃是由 Windows 在 realloc() 行某处触发断点引起的。
如果有什么不同的话,有问题的代码具体来自 SDL_Mixer 的 SVN 版本。
static void add_music_decoder(const char *decoder)
{
void *ptr = realloc(music_decoders, num_decoders * sizeof (const char **));
if (ptr == NULL) {
return; /* oh well, go on without it. */
}
music_decoders = (const char **) ptr;
music_decoders[num_decoders++] = decoder;
}
我使用的是 Visual Studio 2008,music_decoders 和 num_decoders 都是正确的(music_decoders 包含一个指向字符串“WAVE”的指针,music_decoders.ptr 是 0x00000000,据我所知,崩溃似乎是在 realloc 中() 函数。有谁知道我如何处理这个崩溃问题?我不介意必须进行一些重构才能完成这项工作(如果归结为这一点)。
Apparently this function in SDL_Mixer keeps dying, and I'm not sure why. Does anyone have any ideas? According to visual studio, the crash is caused by Windows triggering a breakpoint somewhere in the realloc() line.
The code in question is from the SVN version of SDL_Mixer specifically, if that makes a difference.
static void add_music_decoder(const char *decoder)
{
void *ptr = realloc(music_decoders, num_decoders * sizeof (const char **));
if (ptr == NULL) {
return; /* oh well, go on without it. */
}
music_decoders = (const char **) ptr;
music_decoders[num_decoders++] = decoder;
}
I'm using Visual Studio 2008, and music_decoders and num_decoders are both correct (music_decoders contains one pointer, to the string "WAVE", and music_decoders. ptr is 0x00000000, and the best I can tell, the crash seems to be in the realloc() function. Does anyone have any idea how I could handle this crash problem? I don't mind having to do a bit of refactoring in order to make this work, if it comes down to that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一方面,分配
num_decoders
指针数组,然后写入该数组中的索引num_decoders
是无效的。 大概第一次调用这个函数时,它分配了 0 个字节并写入了一个指向结果的指针。 这可能会损坏内存分配器的结构,从而在调用realloc
时导致崩溃/断点。顺便说一句,如果您报告该错误,请注意
add_chunk_decoder
(在 Mixer.c 中)以同样的方式被破坏。我会替换
为
For one thing, it's not valid to allocate an array of
num_decoders
pointers, and then write to indexnum_decoders
in that array. Presumably the first time this function was called, it allocated 0 bytes and wrote a pointer to the result. This could have corrupted the memory allocator's structures, resulting in a crash/breakpoint whenrealloc
is called.Btw, if you report the bug, note that
add_chunk_decoder
(in mixer.c) is broken in the same way.I'd replace
with
确保 SDL_Mixer.DLL 文件和您的程序构建使用相同的 C 运行时设置。 有可能使用一个 CRT 分配内存,然后使用另一个 CRT 重新分配内存。
在项目设置中,查找 C/C++ -> 代码生成。 两者的运行时库设置应该相同。
Make sure that the SDL_Mixer.DLL file and your program build are using the same C Runtime settings. It's possible that the memory is allocated using one CRT, and realloc'ed using another CRT.
In the project settings, look for C/C++ -> Code Generation. The Runtime Library setting there should be the same for both.
music_decoders[num_decoders++] = 解码器;
你是这里的一员。 如果 num_decoders 是数组的大小,则最后一个索引是 num_decoders - 1。因此,您应该将该行替换为:
music_decoders[num_decoders-1] = detector;
并且您可能希望在函数的开头而不是末尾增加 num_decoders ,因为您想要重新分配新的大小,而不是旧的大小。
另外一件事:您想要将大小与 sizeof (const char *) 相乘,而不是与双星相乘。
music_decoders[num_decoders++] = decoder;
You are one off here. If num_decoders is the size of the array then the last index is num_decoders - 1. Therefore you should replace the line with:
music_decoders[num_decoders-1] = decoder;
And you may want to increment num_decoders at the beginning of the function, not at the end since you want to reallow for the new size, not for the old one.
One additional thing: you want to multiply the size with sizeof (const char *), not with double-star.
啊,C 编程的乐趣。 超出内存块边界的写入可能会触发 realloc(或 malloc 或 free)崩溃 - 这可能发生在程序中的任何其他地方。 我过去使用的方法是调试 malloc 包的一些风格。 在使用第三方解决方案之前,请检查文档以查看 Visual Studio 是否提供了类似的内容。
Ah, the joys of C programming. A crash in realloc (or malloc or free) can be triggered by writing past the bounds of a memory block -- and this can happen anywhere else in your program. The approach I've used in the past is some flavor of debugging malloc package. Before jumping in with a third party solution, check the docs to see if Visual Studio provides anything along these lines.
崩溃通常不是由断点触发的。 您是否崩溃、因断点而中断或在处理断点期间崩溃?
调试输出窗口应该包含一些有关为何命中 CRT 断点的信息。 例如,它可能会在内存操作期间注意到原始块周围的保护字节已被修改(由于在调用 add_music_decoder 之前发生了缓冲区溢出)。 当内存被释放时以及可能在重新分配时,CRT 将检查这些保护页。
Crashes are not generally triggered by breakpoints. Are you crashing, breaking due to a breakpoint or crashing during the handling of the breakpoint?
The debug output window should have some information as to why a CRT breakpoint is being hit. For example, it might notice during the memory operations that guard bytes around the original block have been modified (due to a buffer overrun that occurred before add_music_decoder was even invoked). The CRT will check these guard pages when memory is freed and possibly when realloced too.