Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Failed to load image from memory. Reason: Out of memory  (Read 11757 times)

0 Members and 1 Guest are viewing this topic.

andrew30

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Failed to load image from memory. Reason: Out of memory
« on: July 26, 2021, 02:06:22 pm »
Good day. I made a 64-bit game, everything worked well, I connected all the libraries according to the instructions, then I decided to build the 32-bit version, I set everything up according to the instructions, I connected all the libraries, set the paths to the libraries, the build is successful, but when I run the debug 32x version after some
then after loading the game an error window appears and the console displays: “Failed to load image from memory. Reason: Out of memory ”. In the release version, some time after the download, the game simply crashes. In 64 versions everything works in both release and debug versions. Can anyone know what the problem might be?
First, I create textures on the heap, then load them from memory via a function:
Texture * LoadTextureFromResource (const unsigned short & ID, const char * type)
{
HRSRC rsrcData = FindResource (NULL, MAKEINTRESOURCE (ID), type);
if (! rsrcData)
throw std :: runtime_error ("Failed to find resource.");

DWORD rsrcDataSize = SizeofResource (NULL, rsrcData);
if (rsrcDataSize <= 0)
      throw std::runtime_error("Size of resource is 0.");

   HGLOBAL grsrcData = LoadResource(NULL, rsrcData);
   if (!grsrcData)
      throw std::runtime_error("Failed to load resource.");

   LPVOID firstByte = LockResource(grsrcData);
   if (!firstByte)
      throw std::runtime_error("Failed to lock resource.");
Texture *texture = new Texture;
   //LOGGER(rsrcDataSize)
   //   LOGGER(firstByte)
   //   LOGGER(texture != nullptr)
   cout<<ID<<endl;
   if (!texture->loadFromMemory(firstByte, rsrcDataSize))
      throw std::runtime_error("Failed to load image from memory.");
      //return nullptr;
   //return Texture();
   return texture;
}
In total I have about 684+ - textures

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Failed to load image from memory. Reason: Out of memory
« Reply #1 on: July 26, 2021, 02:34:49 pm »
What kinds of resolutions are the 684 images?
A 1024x1024 32 bit colour image takes 4MB of ram.
Resources loaded by LoadResource() remain in ram until the module they came from (in this case the main program) is unloaded, so there's 2 copies of every texture (one as a windows resource and one as an sfml texture).
If we assume 1024x1024 textures, that's 4MB * 684 * 2. That's 5.7GB of ram. 32bit programs on windows only have access to 2GB of ram by default (not 4GB, it reserves have the space for the OS), so you'd run out of ram. You can boost this to around 3.5GB by setting the LARGEADDRESSAWARE flag when compiling or using a tool to set it on an existing program, but that's still not enough.

https://docs.microsoft.com/en-us/cpp/build/reference/largeaddressaware-handle-large-addresses?view=msvc-160

andrew30

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Failed to load image from memory. Reason: Out of memory
« Reply #2 on: July 26, 2021, 03:02:26 pm »
I am using textures with a resolution of 1920 x 1080 in PNG format. But their weight is small, about 30 - 200 KB

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Failed to load image from memory. Reason: Out of memory
« Reply #3 on: July 26, 2021, 04:45:45 pm »
1920x1080 images as 32bit colour take 8.1MB each. The file type and compression have no effect, they must be fully decompressed to raw in memory before the graphics card can understand them.


andrew30

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Failed to load image from memory. Reason: Out of memory
« Reply #4 on: July 26, 2021, 05:25:05 pm »
That is, I have to redo the textures and compact the images in one file?

andrew30

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Failed to load image from memory. Reason: Out of memory
« Reply #5 on: July 26, 2021, 05:28:13 pm »
I noticed that while loading my game, initially 30% of the memory was taken up, after loading it became 90% busy, and the game process itself takes 60+ megabytes, and does not show a process that could take 60% of the memory

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Failed to load image from memory. Reason: Out of memory
« Reply #6 on: July 27, 2021, 12:17:38 am »
Are you trying to load all of these textures into memory at the same time? If so, why not only load the textures that are actively being used? Then once you are done with those, you can free that memory before loading in the next set.

As Kojack mentioned, png image files use compression. They might only be 30 - 200 KB on disk, but they will be much larger when loaded in memory and uncompressed behind the scenes.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Failed to load image from memory. Reason: Out of memory
« Reply #7 on: August 05, 2021, 12:10:17 pm »
By the description of things, you do simply run out of memory: 684 textures * 8MB = 5.3GB of memory
Which for a 32bits application, won't fit into the limit of 4GB of memory anymore.
As others have mentioned, you probably don't need all of these textures at the same time or continue buidling 64bits application.

Also I'm seeing you call new Texture in your code without a deletion, which may very well lead to memory leaks. ;)
For example, you could be calling new Texture in a loop, thus filling the memory more and more with new objects that you never delete.

You should avoid manual memory management with new and delete and instead work with stack variables, references and smart pointers.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

andrew30

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Failed to load image from memory. Reason: Out of memory
« Reply #8 on: August 14, 2021, 06:45:31 pm »
Thank you all for your help, I created texture atlases and now textures take up much less memory and the animation rendering performance has increased and everything works in 32-bit mode. The game is simple, so it doesn't make sense to load textures into memory on demand.

Asimov

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Failed to load image from memory. Reason: Out of memory
« Reply #9 on: July 27, 2024, 04:05:03 am »
I am also trying to load from a resource file.
Could you give me your full resource code so I can work out how to do it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Failed to load image from memory. Reason: Out of memory
« Reply #10 on: July 28, 2024, 02:40:50 pm »
Please don't post on existing threads with unrelated topics.

Loading from Windows resource files is something different than just loading from memory and unrelated to the original topic.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/