As the title suggests, I have run across an issue of something working in the Debug run when it runs with the IDE, but if I run the release version without the IDE, it fails.
I have a GUI (main menu) that leads to the rest of the program when a button is clicked. It'll run until I click that button.
I have narrowed it down to this line of code, but I'm not sure why it won't run it.
parent->game_state = ClassA::GameState::in_game;
Game_state is an enumerator that is inside of ClassA. This line of code is being called from a child of ClassA.
I feel like the problem lies in having to include the files for the classes inside of each other, but I couldn't think of another way to give the child class access to the enumerator.
Usually the "works in debug but not release" problem comes down to some kind of uninitialized variable issue. This is because all variables are automatically initialized to zero in debug mode and this could mask a problem where a garbage value is used.
When the button is clicked it runs this function. It's very short. Basically, engine is a pointer inside of ClassA. The main menu is also inside of ClassA, so I passed ClassA to the main menu, so I could change the state of the program from on_main_menu to in_game.
Is parent a valid pointer? What about the StartMenu object, is that valid? Sometimes calling a member function on an invalid object pointer will cause the crash to happen inside of the function once it uses member variables.
You have a circular include there, start_menu.h includes class_a.h which includes start_menu.h which includes class_a.h and so on. Not sure why that compiles...
What does the Engine constructor look like? All the code here looks fine, provided nothing else gets run in between.
Engine::Engine() {
running = true;
take_input = true;
std::ifstream map_list("MapList.txt");
for (unsigned i = 0; map_list.good(); ++i) {
string temp;
getline(map_list, temp);
try {
maps.push_back(new Map(temp)); }
catch (std::bad_alloc) {
std::cout << "std::bad_alloc | Failed to allocate memory to create map in engine." << std::endl; }
}
map_list.close();
current_map = 0;
tile_dimensions = sf::Vector2i((int)maps[current_map]->GetTileWidth(), (int)maps[current_map]->GetTileHeight());
try {
connor = new Connor("Characters/Connor"); }
catch (std::bad_alloc) {
std::cout << "std::bad_alloc | Failed to allocate memory to create Connor in engine." << std::endl; }
connor->SetGlobalCoords(4 * tile_dimensions.x, 4 * tile_dimensions.y);
}
Here is the Engine constructor. It's worked forever, so I don't know if anything would be wrong here.
However, back when I programmed on Code::Blocks (another IDE, if you didn't know), I first discovered circular includes and that IDE wouldn't compile with them. MS VS2010 does however, so I used it. I can't think of another way to get a pointer to the parent, other than what I did. :/
Is there another way to do that? Maybe without circular includes?
If you ran your code using a debugger (i.e. the release build, with debugging symbols added, under a debugger - not the debug build, which is clearly different) it would tell you the exact line causing the problem and you would be able to interrogate the variables to find the culprit.
If you only need a pointer or a reference, then all you have to do is forward declare the class ( class Parent; ) then just include the header from the source file of the class that needs it.
When you're loading the maps for the engine, change the loop to:
1 2 3 4
while (getline(map_list, temp))
{
//stuff
}
That way you don't have the one extra loop at the end, or the extra overhead of the for loop where it isn't needed. This *might* be the cause of the crash, but other than that I really have no idea what else could be causing it.