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

Author Topic: Semi-random crashes when creating Textures  (Read 17240 times)

0 Members and 2 Guests are viewing this topic.

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Semi-random crashes when creating Textures
« on: November 12, 2018, 09:34:35 am »
Originally my game uses sprites/textures exported for 1080p and recently I added support for a high resolution sprite option (4K). I noticed that after a while the game always crashes when trying to load a random texture. I can get the crash to happen almost consistently when entering the final boss area of my game (since it loads quite a lot of large textures in a row there). The game doesn't appear to be using very much memory at all when it crashes and the textures are still quite a lot smaller then the GPU's max texture size, so I'm not sure what the problem is.

The crash is generally from a LoadingFailedException but it also sometimes throws a SEHException:
Quote
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in sfmlnet-graphics-2.dll
External component has thrown an exception.

An unhandled exception of type 'SFML.LoadingFailedException' occurred in sfmlnet-graphics-2.dll
Failed to load texture from memory
I've gotten a few LoadingFailedException (failed to load texture) crash reports from users in the past as well (using the normal 1080p graphics).

The game loads the textures in via MemoryStreams (extracted from uncompressed zip files) but I also tested loading from filenames and the problem persisted.

Has anyone else had problems like this and/or know how I could fix this?

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Semi-random crashes when creating Textures
« Reply #1 on: November 13, 2018, 06:25:52 am »
I personally have not seen this exception while using SFML before.

Are you able to provide a complete and minimal example that reproduces the issue?

Edit: Possibly related to https://en.sfml-dev.org/forums/index.php?topic=24449.0 what are your texture file types?
« Last Edit: November 13, 2018, 06:28:48 am by Gleade »

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #2 on: November 13, 2018, 09:14:22 am »
All the images used are pngs, most with transparency.

I can get the exception simply by making a loop that constantly makes new textures.

Code: [Select]
for (int i = 0; i < 400; i++)
{
    Texture texture = new Texture("test.png");
}

The crash occurs after looping around 40-120 times. I tried with different images, ones with and without transparency, pngs, jpegs and gifs. When it crashes the programs is hardly using any Memory (less than 100MB) and less than 1% of the GPU.

I can stop that test code from crashing if I add all the textures to a list and start disposing them if the list becomes larger than 20. But it should be able to handle much more than that.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Semi-random crashes when creating Textures
« Reply #3 on: November 13, 2018, 09:22:17 am »
Does it happen from C (CSFML) or C++ (SFML) too?
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Semi-random crashes when creating Textures
« Reply #4 on: November 13, 2018, 09:58:41 am »
Quote
When it crashes the programs is hardly using any Memory (less than 100MB) and less than 1% of the GPU
What about GPU memory? What's the total size loaded when it crashes (uncompressed, ie. width * height * 4 bytes for each image)?
Laurent Gomila - SFML developer

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #5 on: November 13, 2018, 10:35:50 am »
According to Task Manager the GPU Memory doesn't go up much at all (was 1.7/19.0 GB before launch and stayed there). The main image I'm testing with is 4782x2064, and it crashes after loading it for the 40th time.

I tested the same thing in SFML and there was no problems. With CSFML I got this exception:
Quote
Exception thrown at 0x772A17D2 in test.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0133F6D8

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Semi-random crashes when creating Textures
« Reply #6 on: November 13, 2018, 11:24:38 am »
Is is always after the 40th time, or is it random? std::bad_alloc means that memory allocation failed on the C++ side (SFML), so it's strange that testing it directly with SFML worked. Have you tried multiple times to be sure?
Laurent Gomila - SFML developer

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #7 on: November 13, 2018, 12:01:31 pm »
With that particular image it's always after the 40th time. Though another image I tested get's it randomly between the 125th and 130th time.

I can get SFML to load the texture in hundreds of times in a row with no problems at all. Though if I change the code to create and load images instead of textures; I get the std::bad_alloc.

Code: [Select]
// no problems:
for (int i = 0; i < 200; i++)
{
sf::Texture texture;
if (texture.loadFromFile("image.png"))
textures[i] = texture;
}
// crashes after loading 40:
for (int i = 0; i < 200; i++)
{
sf::Image image;
if (image.loadFromFile("image.png"))
images[i] = image;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Semi-random crashes when creating Textures
« Reply #8 on: November 13, 2018, 01:31:45 pm »
This is expected: when you keep textures in memory, they are stored in GPU RAM and can't cause std::bad_alloc. When you keep images, they are stored in system RAM and eventually cause std::bad_alloc after a while.

But this scenario doesn't match your initial code, where only textures were kept in memory.
Laurent Gomila - SFML developer

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #9 on: November 13, 2018, 02:55:02 pm »
Here's the CSFML test code I used which results in the std::bad_alloc:
Code: [Select]
for (int i = 0; i < 200; i++)
{
sfTexture* texture = sfTexture_createFromFile("image.png", NULL);
}

I assume the std::bad_alloc there and the LoadingFailedException I get in the SFML.Net test are related.
It's weird that doing in directly in SFML (C++) works without any problems though...

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Semi-random crashes when creating Textures
« Reply #10 on: November 13, 2018, 03:20:30 pm »
How did you do it in C++? I ask in case maybe you allocate it in loop as a local.

This is really strange since only thing C does differently is allocating a small struct that holds a bool and a pointer:

https://github.com/SFML/CSFML/blob/master/src/SFML/Graphics/TextureStruct.h

https://github.com/SFML/CSFML/blob/master/src/SFML/Graphics/Texture.cpp
« Last Edit: November 13, 2018, 04:26:45 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #11 on: November 13, 2018, 05:23:29 pm »
The code is pretty much the same for all the tests. I even made it so it adds the textures to an array and the SFML test can still load in hundreds without any problems.

SFML (C++), no issues:
Code: [Select]
for (int i = 0; i < 200; i++)
{
sf::Texture texture;
if (texture.loadFromFile("image.png"))
textures[i] = texture;
}

SFML.Net, eventually results in LoadingFailedException:
Code: [Select]
for (int i = 0; i < 200; i++)
{
    Texture texture = new Texture("image.png");
}

CSFML, eventually results in std::bad_alloc:
Code: [Select]
for (int i = 0; i < 200; i++)
{
sfTexture* texture = sfTexture_createFromFile("image.png", NULL);
}

Same things happens for the SFML.Net and CSFML tests if I add the textures to lists/arrays like the SFML one.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Semi-random crashes when creating Textures
« Reply #12 on: November 13, 2018, 06:24:06 pm »
Nevermind my previous comment, I didn't look at that assignment. What is textures in your C++ code? And this still is very strange.
Back to C++ gamedev with SFML in May 2023

Torrunt

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Torrunt
Re: Semi-random crashes when creating Textures
« Reply #13 on: November 13, 2018, 06:30:21 pm »
It's just an array ( sf::Texture textures[400]; ).

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Semi-random crashes when creating Textures
« Reply #14 on: November 13, 2018, 06:43:07 pm »
Well, the only difference between that and C/.NET is that they allocate dynamically and the CSFML allocates a sf::Texture and a small struct but both sf::Texture and that struct in CSFML are so small they shouldn't even matter so what gives with this memory error..? :o ???
Back to C++ gamedev with SFML in May 2023