SFML community forums

Help => Graphics => Topic started by: ismetteren on September 20, 2009, 01:29:44 pm

Title: Program crashes on Image.LoadFromFile()
Post by: ismetteren on September 20, 2009, 01:29:44 pm
i have this code:
Code: [Select]
Image* image;
std::cout << "test64" << std::endl;
//Problem with is with the load from file function!!
if(image->LoadFromFile(path)) {
std::cout << "test123" << std::endl;
}
else {
std::cout << "testawd123" << std::endl;
}
std::cout << "test65" << std::endl;
imageLibary[path] = image;
std::cout << "test66" << std::endl;

As you can see, i cant really get my debugger to work, but i have debugged the program using alot of cout instead.

the program stops responding on the line "if(image->LoadFromFile(path)) {"

It dosent even output any of the two possible outputs from the if statement.

What can the problem be? shouldent a non-existing file path just make the function return 0 so my program could go on? (this might be the problem, since im not sure from what file i should start my realative filepath. The sourcefile, the .o file or the .exe file?)
Title: Program crashes on Image.LoadFromFile()
Post by: Spidyy on September 20, 2009, 01:45:41 pm
Where did you instanciate your Image? Here you juste create a pointer pointing to nowhere. :p

You should instanciate it with a "new sf::Image()" or simply create a sf::Image object instead of pointer.
Title: Program crashes on Image.LoadFromFile()
Post by: Nexus on September 20, 2009, 01:57:05 pm
Dereferencing a not initialized pointer results in undefined behaviour, so nearly everything can happen. That's why neither of your if/else statements is reached.

I don't see a reason to use the heap (free store) here, so you can just put the sf::Image on the stack, as Spidyy stated:
Code: [Select]
sf::Image Image;
if (Image.LoadFromFile(path))
{
    // ...
}
else
{
    // ...
}
Title: Program crashes on Image.LoadFromFile()
Post by: ismetteren on September 20, 2009, 02:20:06 pm
Thank you very much for the quick answer!

It works now :D
Title: Program crashes on Image.LoadFromFile()
Post by: Hiura on September 20, 2009, 03:13:31 pm
Quote from: "ismetteren"
It works now :D
But did you understand why and what did you do wrong ?

( I'm just asking 'cause lot of people don't understand what they do and come again and again here about the same problem. I'm not saying you're stupid. ;) )
Title: Program crashes on Image.LoadFromFile()
Post by: LGV on September 23, 2009, 10:44:30 pm
Why people use pointers everywhere? It's like an epidemy :P
Title: Program crashes on Image.LoadFromFile()
Post by: forrestcupp on September 30, 2009, 11:10:00 pm
Quote from: "LGV"
Why people use pointers everywhere? It's like an epidemy :P
People use pointers everywhere because we're trained to in other frameworks, like wxWidgets & Ogre3D.  People are just mindlessly trained that way, and they never actually learn what pointers are really for.

It's actually a breath of fresh air to come across something like SFML where you don't have to use pointers unless they're actually needed.