SDL_LoadBMP 间歇性失败
我在循环中调用 SDL_LoadBMP("duck.bmp") 一万次。
大约第一千次之后,调用失败并且 SDL_GetError() 报告:
“无法打开 duck.bmp”
我不明白这是为什么——我可以做些什么来获取更多信息吗?
听起来可能是内存问题,但发生这种情况时,系统内存中有足够的可用空间。
注:BMP 为 32x32。
I am calling SDL_LoadBMP("duck.bmp") in a loop ten thousand times.
After about the thousandth time, the call fails and SDL_GetError() reports:
"Couldn't open duck.bmp"
I can't figure out why this is -- is there anything I can do to get more information?
It sounds like perhaps it may be a memory issue, but there is plenty of system RAM free when this occurs.
Note: the BMP is 32x32.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使您有足够的可用系统 RAM,仍然可能会耗尽地址空间;在 32 位应用程序中,您通常只能使用 2GB。虽然图像那么小,但应该需要 1000 次以上才能用完那么多内存。您在循环中是否做了其他需要占用内存的事情?
最重要的是,您是否有理由要重新加载图像文件 10,000 次?如果您正在寻找要操作的图像的多个副本,我建议使用 SDL_ConvertSurface 而不是每次都返回到文件。如果此方法也失败,则 SDL_GetError 可能会在失败时为您提供更有意义的错误消息。
如果您还将数据写回该文件,请确保正确关闭它,否则可能会遇到权限问题。我非常确定 Windows 不会允许您打开已打开用于写入的文件进行读取。 (这似乎不太可能,因为您只是在循环迭代一千次后才遇到问题,但值得检查。)
Even if you have plenty of free system RAM, could still run out of address space; you generally only get 2GB to work with in a 32-bit application. Although with an image that tiny, it ought to take way more than 1000 times to use up that much memory. Are you doing anything else memory-hungry in your loop?
Most importantly, is there a reason you want to re-load the image file 10,000 times? If you're looking for multiple copies of the image to manipulate, I'd recommend making copies of the original surface with SDL_ConvertSurface instead of going back to the file each time. If this method fails as well, it's possible that SDL_GetError will give you a more meaningful error message when it does.
If you are also writing data back to that file, make sure you're properly closing it, or you might be running into a permissions sort of issue. I'm pretty sure that Windows won't allow you to open a file for reading that is already open for writing. (This seems less likely since you're only hitting the problem after a thousand iterations of your loop, but it's worth checking.)
处理完图像后,您应该调用 SDL_FreeSurface(请参阅 http://wiki.libsdl.org/SDL_FreeSurface< /a>)。否则,内存不会被释放。
正如 Raptor007 指出的那样,不建议加载图像 1000 次。我以为你这样做是为了看看是否存在内存泄漏。如果没有...停止这样做。一次就够了。
When you're done with the image, you should call SDL_FreeSurface (see http://wiki.libsdl.org/SDL_FreeSurface). Otherwise, well, the memory is not freed.
As Raptor007 points out, loading an image 1000 times is, ahem, not recommended. I assumed you were doing this to see if there was a memory leak. If not... stop doing it. Once is enough.