SFML community forums
Help => Graphics => Topic started by: ismetteren on September 20, 2009, 01:29:44 pm
-
i have this code:
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?)
-
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.
-
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:
sf::Image Image;
if (Image.LoadFromFile(path))
{
// ...
}
else
{
// ...
}
-
Thank you very much for the quick answer!
It works now :D
-
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. ;) )
-
Why people use pointers everywhere? It's like an epidemy :P
-
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.