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

Author Topic: Program crashes on Image.LoadFromFile()  (Read 3740 times)

0 Members and 1 Guest are viewing this topic.

ismetteren

  • Newbie
  • *
  • Posts: 2
    • View Profile
Program crashes on Image.LoadFromFile()
« 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?)

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Program crashes on Image.LoadFromFile()
« Reply #1 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Program crashes on Image.LoadFromFile()
« Reply #2 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
{
    // ...
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ismetteren

  • Newbie
  • *
  • Posts: 2
    • View Profile
Program crashes on Image.LoadFromFile()
« Reply #3 on: September 20, 2009, 02:20:06 pm »
Thank you very much for the quick answer!

It works now :D

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Program crashes on Image.LoadFromFile()
« Reply #4 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. ;) )
SFML / OS X developer

LGV

  • Newbie
  • *
  • Posts: 11
    • MSN Messenger - laureano_river@hotmail.com
    • View Profile
Program crashes on Image.LoadFromFile()
« Reply #5 on: September 23, 2009, 10:44:30 pm »
Why people use pointers everywhere? It's like an epidemy :P

forrestcupp

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Program crashes on Image.LoadFromFile()
« Reply #6 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.