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

Author Topic: Seg fault in loadFromFile  (Read 12393 times)

0 Members and 3 Guests are viewing this topic.

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Seg fault in loadFromFile
« on: July 23, 2013, 01:57:29 pm »
Lubuntu 13.04 64 bit, g++ 4.7, SFML2 from github (very recent build).

#include <SFML/Graphics.hpp>

int main()
{
sf::Texture tex;
text.loadFromFile("data/");  //CRASH if data folder actually exists
//alternative version:
text.loadFromFile("data"); //CRASH if data folder actually exists
}
 

There will be seg fault on loadFromFile when the dir actually exist. When the folder "data" doesn't exist in app's location there is no seg fault. It may be linux-only as well.

Call stack:
Quote
0  0x00007ffff78bf66c  stbi_load   
1  0x00007ffff78c33f4  sf::priv::ImageLoader::loadImageFromFile(std::string const&, std::vector<unsigned char, std::allocator<unsigned char> >&, sf::Vector2<unsigned int>&)   
2  0x00007ffff78c8843  sf::Texture::loadFromFile(std::string const&, sf::Rect<int> const&)   

I encountered this bug in following situation: (so it's a bug which may affect some ppl in rare situations)
sf::Texture tex;
std::string textureDir = "data/";
std::string textureName = map.getTextureName(..); // by error in map file, it returned empty string

tex.loadFromFile(textureDir+textureName);
 

What I expect in above case is that loadFromFile will return false and I will have an empty texture.
However, it simply crashed.

Remember that it must be existing folder as the function argument. When I set:
textureDir = "muhahadfjs2343434/";
 

Then there is no crash and expected output in console:
Quote
Failed to load image "muhahadfjs2343434/". Reason : Unable to open file


I hope that you will look into it. I can provide more debug info if you want.
« Last Edit: July 23, 2013, 01:59:25 pm by netrick »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Seg fault in loadFromFile
« Reply #1 on: July 23, 2013, 02:01:07 pm »
So, to summarize, the loadFromFile function crashes if you give it the path of a directory rather than a file?

Can you compile SFML in debug mode to get more information from the debugger (especially the line of the crash)?
Laurent Gomila - SFML developer

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Seg fault in loadFromFile
« Reply #2 on: July 23, 2013, 02:01:44 pm »
Yes, if you give it the path to existing directory.

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Seg fault in loadFromFile
« Reply #3 on: July 23, 2013, 02:02:34 pm »
Can you compile SFML in debug mode to get more information from the debugger (especially the line of the crash)?

Sure, if you could tell me cmake command to compile it in debug mode?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Seg fault in loadFromFile
« Reply #4 on: July 23, 2013, 02:08:51 pm »
Set CMAKE_BUILD_TYPE to Debug.

It is explained in the tutorial very clearly.
Laurent Gomila - SFML developer

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Seg fault in loadFromFile
« Reply #5 on: July 23, 2013, 02:12:52 pm »
When I link SFML libs in debug mode there is no crash and I get:
Quote
Failed to load image "data". Reason : Image not of any known type, or corrupt

The crash happens only with release SFML libs.

Very strange. So I can't provide you the line of crash because with debug libs there is no crash.

EDIT:

But looking at the loadFromFile() source I know the line of crash:
106:    unsigned char* ptr = stbi_load(filename.c_str(), &width, &height, &channels, STBI_rgb_alpha);
 
It is the last function called in release mode.

I have no idea what STBI is. But it may be upstream bug in release mode.

EDIT2:

I see that STBI is packaged with SFML source and I think it has no active bug tracker. So it looks like it's the issue we need to fix ourselves. It works in debug mode, so we need to find out what is the difference between release and debug mode in STBI.
« Last Edit: July 23, 2013, 02:24:08 pm by netrick »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Seg fault in loadFromFile
« Reply #6 on: July 23, 2013, 02:25:38 pm »
The crash is inside the stbi_load function, but you don't know which at line exactly (it's not the call itself which crashes, it's something inside).

It's very strange that it works in debug mode... I'll try to investigate that myself.
Laurent Gomila - SFML developer

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Seg fault in loadFromFile
« Reply #7 on: July 23, 2013, 02:32:25 pm »
It's very strange that it works in debug mode... I'll try to investigate that myself.

That's good to hear. I don't feel like I'm able to solve it myself. STBI code is nothing like SFML code you know.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Seg fault in loadFromFile
« Reply #8 on: July 23, 2013, 02:44:50 pm »
Yes. STBI code is kind of scary ;D
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Seg fault in loadFromFile
« Reply #9 on: July 23, 2013, 07:10:20 pm »
After checking, fopen works when path is a directory and access is read only. That's because, well, everything is a file on Unixes.

What's annoying is that there's no standard and easy way of checking whether the path (or the open FILE*) refers to a file or a directory; one would have to call stat() first for example.
Laurent Gomila - SFML developer

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Seg fault in loadFromFile
« Reply #10 on: July 23, 2013, 07:12:30 pm »
So we need to add some IFDEF for Unix and call some unix functions to check if it's dir or file?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Seg fault in loadFromFile
« Reply #11 on: July 23, 2013, 07:41:11 pm »
Quote
So we need to add some IFDEF for Unix and call some unix functions to check if it's dir or file?
Probably/possibly, it's really few lines and easy to do but it's kind of trolling the dev when a directory opens for reading images from it. :D

Quote
That's because, well, everything is a file on Unixes.
That is SO funny.(Not the fact everything is a file, I knew that, but the fact that it came in such lol context and caused such a  bug :D).
 
Quote
What's annoying is that there's no standard and easy way of checking whether the path (or the open FILE*) refers to a file or a directory; one would have to call stat() first for example.
Time to implement SFML.Filesystem! 8) ;D
Back to C++ gamedev with SFML in May 2023

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Seg fault in loadFromFile
« Reply #12 on: July 23, 2013, 07:48:06 pm »
Time to implement SFML.Filesystem! 8) ;D

Haha that's true - for example I don't know any cross platform way for example to list files in a dir in C++ (only huge I-take-10-mins-to-compile-your-app boost).

Atani

  • Newbie
  • *
  • Posts: 30
    • MSN Messenger - atanisoft@hotmail.com
    • AOL Instant Messenger - AtaniSYSOP
    • Yahoo Instant Messenger - atanisoft
    • View Profile
    • Email
Re: Seg fault in loadFromFile
« Reply #13 on: July 23, 2013, 08:00:11 pm »
Time to implement SFML.Filesystem! 8) ;D

Haha that's true - for example I don't know any cross platform way for example to list files in a dir in C++ (only huge I-take-10-mins-to-compile-your-app boost).

Instead of add something to SFML like this, why not use PhysFS?  There is an sf::InputStream implementation or two around that work with PhysFS and SFML2.

An added benefit to this would be packaging your resources etc in archives (save disk space) and reduce complexities in listing resources in a multi-platform way.  And yes PhysFS does support reading straight from a directory.

netrick

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Re: Seg fault in loadFromFile
« Reply #14 on: July 23, 2013, 08:03:43 pm »
@Atani

That's very interesting, I didn't know that lib! I will look into it for sure.

However the bug in SFML still needs to be fixed :P I think that IFDEF solution will work, who would look into so ugly STBI code anyway? :P

 

anything