SDL_LoadBMP fails intermittently
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.
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.)
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.
精彩评论