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

Author Topic: LoadFromFile Causing a Crash  (Read 1655 times)

0 Members and 2 Guests are viewing this topic.

Ikizami

  • Newbie
  • *
  • Posts: 3
    • View Profile
LoadFromFile Causing a Crash
« on: September 22, 2011, 10:00:35 pm »
Alright, this is my first time posting on the forums, so I apologize if I do something unorderly or incorrectly. I've been having an issue with using LoadFromFile with an Image, while it's in a class, and not in main.

For example, in main()
Code: [Select]
sf::Image Image;
Image.LoadFromFile("Bunny.png");
sf::Sprite Sprite;
Sprite.SetImage(Image);


This works perfectly, and how it should. I can further on use "Sprite" to draw the "Bunny.png" and everything else. With the Draw(Sprite); function, and such.

However, in a larger game I'm working on, I've made an image manager class (so I can handle, load, and set images in just one little file, instead of in every class, monster, level, etc). In my larger game, it works perfectly, but in this tiny application I'm attempting to make, anytime I call "LoadFromFile" while it's not in main, it will crash.

In my ImageManager class, I have the following:
Code: [Select]
char FilePath[256];
for(int i = 0; i < 5; i++)
{
    switch(i)
    {
        case 0:
            strcpy(FilePath, "./Data/Images/TitleBackground.png");
            break;
        default: ;
    }
    Image[i].LoadFromFile(FilePath);
}


For whatever reason, it crashes when it gets to "LoadFromFile()." Which has me dumbfounded, because I have used the exact same code -- with the exception of the specific files I'm loading, of course -- and it still produces the crash. I have tried switching the images, moving the images into other locations. Making the image be direction in the same folder, not in a "Data/Images/" directory, I've double checked to make sure I've linked everything properly with the -lsfml-system, graphics, audio, etc; nothing is making any sense. Aside from the project's files, settings-wise, it is identical to my other project that works; however, I can't for the life of me, figure out why this one refuses to work, and produces a crash.

If I am calling LoadFromFile while in main.cpp, it works as it should, but in any other file, such as ImageManager.cpp, it does not. I am using Code::Blocks. Everything (that I know of) is linked properly, and my code should be working correctly, considering I copy and pasted it verbatim from my other project. Compiling gives no errors nor no warnings; however, I know good and well that doesn't mean it will be flawless code, but the crash doesn't make any sense from me. The console window does not say anything, so I can assume it's working fine; it just crashes whenever LoadFromFile is called outside of main.cpp.

I have went as far as to simply copy

Code: [Select]
Image.LoadFromFile("Bunny.png");
Sprite.SetImage(Image);


into the ImageManager's loading function (just to test if it was a code error), but it crashes as well. While having "sf::Image Image;" and "sf::Sprite Sprite;" defined in the ImageManager.h class, of course, under private. It still crashes with no info or insight into what's causing this crash.

This started out as a tiny little project to make something for a friend over night, and it's turned into a five-hour headache for me.

Haikarainen

  • Guest
LoadFromFile Causing a Crash
« Reply #1 on: September 22, 2011, 10:13:32 pm »
How does it crash? Segmentation fault? Post stacktrace.

Also; Make sure the files you're trying to load are in the right directory! If you i.e use codeblocks, the binaries are default in projectdir/bin/debug. If you run the executable in the debugdir and the datadirectory is in the debugdir as well, it would work, but if you run it from your IDE(codeblocks) it will look for the datadirectory in the projectdir instead of bin/debug.

Ikizami

  • Newbie
  • *
  • Posts: 3
    • View Profile
LoadFromFile Causing a Crash
« Reply #2 on: September 22, 2011, 10:55:03 pm »
When it crashes, in Code::Blocks it shows:
Code: [Select]
Process terminated with status -1073741819 (0 minutes, 4 seconds)


While saying "test.exe has stopped working" then giving the "Check online for a solution" or "Close the program" options.

The files I'm trying to load are in the correct directory, unless I'm completely oblivious to what I've placed. I've had them in the correct directory for running it through Code::Blocks with a build there, I've also tried having everything (including the .dlls) in the debug and release folders, and trying to run it from there as well; which still produces the same crash with the following "has stopped working, check online or close the program" error.

Ikizami

  • Newbie
  • *
  • Posts: 3
    • View Profile
LoadFromFile Causing a Crash
« Reply #3 on: September 25, 2011, 05:43:23 am »
Well, after creating yet a fifth try and waiting a day and a half, it's somehow working again. I suppose this topic can be safely ignored, since it was apparently entirely on my end; even though I can't figure out what did cause it.

 

anything