Using a while(true) loop causes Visual C++ to fail debug assertion
14 replies, posted
Whenever I use a while(true) infinite loop in a certain project, Visual C++ causes a debug assertation to fail.
It has to be something in my code, since it only happens within this certain project. Here's the entire project:
[url=http://www.2shared.com/file/7014199/f591d694/SFML_Code_Base.html]Download Link[/url]
PROTIP: It uses SFML.
Man, I don't want to download, unpack, and sort through all of your code just to help you. I think we would all rather you post the relevant section of code in your OP.
Oh, ok.
Here's main.cpp:
[php]
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include "Engine_PointOfContact.hpp"
#include <iostream>
int main()
{
Necro::Internal::AppSettings n_sets;
n_sets.SetScreenX(640);
n_sets.SetScreenY(480);
n_sets.SetWindowCaption("Necro Engine Test");
Necro::Manager::GameManager gm(n_sets);
while(true)
{
gm.Update();
//continue;
}
std::cout << "Looping over.\n";
return 0;
}
[/php]
GameManager.cpp
[php]
#include "Engine_GameManager.hpp"
//Defines the functions for the GameManager class
using namespace Necro::Manager;
GameManager::GameManager(Necro::Internal::AppSettings settings)
{
Load(settings);
}
void GameManager::Load(Necro::Internal::AppSettings settings)
{
//Create a render window
//NOTE: Don't tamper with the assigning code.
sf::RenderWindow temp_window = sf::RenderWindow(sf::VideoMode(settings.GetScreenX(),settings.GetScreenY(),32),settings.GetWindowCaption());//32 is a placeholder
this->render_window = &temp_window;
//Set the class settings to the ones given by the constructer
this->set = settings;
//call Update()
this->Update();
}
void GameManager::Update()
{
//do
//{
//if(!rw_event)
sf::Event rw_event;
while(render_window->GetEvent(rw_event))
{
if (rw_event.Type == sf::Event::Closed)
{
//render_window->Close();
}
}
render_window->Clear();
render_window->Display();
//}
//while(this->render_window->IsOpened());
std::cout << "Looped.\n";
//if(this->render_window->IsOpened())
//{
//this->Update();
//}
}
void GameManager::Close()
{
render_window = NULL;
delete render_window;
}
GameManager::~GameManager()
{
Close();
}
[/php]
Engine_GameManager.hpp
[php]
#ifndef GAMEMANAGER_HPP
#define GAMEMANAGER_HPP
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include "Mixin_MLoadUpdateClose.hpp"
#include "Engine_AppSettings.hpp"
#include "Engine_Entity.hpp"
namespace Necro
{
namespace Manager
{
class GameManager
{
public:
GameManager();
GameManager(Necro::Internal::AppSettings settings);
~GameManager();
void Load(Necro::Internal::AppSettings settings);
void Update();
void Close();
sf::RenderWindow * GetWindow() { return render_window;}
private:
std::vector<Necro::Entity::Entity*> EntityList;
sf::RenderWindow * render_window;
Necro::Internal::AppSettings set;
};
}
}
#endif
[/php]
Mixin_MLoadUpdateClose
[php]
#ifndef MLOADUPDATECLOSE_HPP
#define MLOADUPDATECLOSE_HPP
//Mixin - Load, Update, Close
//Adds a common ground for three commonn function
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
namespace Necro
{
namespace Mixin
{
class MLoadUpdateClose
{
public:
void Load();
void Update();
void Close();
};
}
}
#endif
[/php]
Engine_AppSettings.hpp
[php]
#ifndef APPSETTINGS_HPP
#define APPSETTINGS_HPP
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <fstream>
#include <string>
//AppSettings:
//Allows a one-class entry point for various app settings
//such as window dimensions, resolution, scale, and such.
namespace Necro
{
namespace Internal
{
class AppSettings
{
public:
AppSettings(){Load("data/application_data.bin");}
void Load(const char * filename);
int GetScreenX() { return screen_x;}
int GetScreenY() { return screen_y;}
void SetScreenX(int n_x) { this->screen_x = n_x;}
void SetScreenY(int n_x) { this->screen_y = n_x;}
std::string GetWindowCaption() { return this->window_caption; }
void SetWindowCaption(std::string n_x) { this->window_caption = n_x; }
private:
int screen_x;
int screen_y;
//int resolution;
//int scale;
std::string window_caption;
//std::fstream file;
};
}
}
#endif
[/php]
Not your whole project, but [b]THE RELEVANT PORTIONS[/b].
+ learn to use the debugger. Hit debug, and MSVC will tell you exactly where the error occured/
That is the relevant portion. And all the debugger tells me is that no symbols were loaded for any dll and that it can't show me any source code.Thatt doesn't help
Dude, the setScreen stuff is not a relevant portion of your code. In fact it seems the entire AppSettings thing could be removed. I'd scan through but really there's too much stuff, as well as commented code that shouldn't be there in the first place.
A good test case is the [b]least[/b] amount of code that triggers the issue. If you can remove something and it still bugs, strip it out.
Ok, thanks. The reason I didn't remove anything from the post is that people have gotten angry at me for not posting enough.
[QUOTE=MrBob1337;16501601]And all the debugger tells me is that no symbols were loaded for any dll and that it can't show me any source code.Thatt doesn't help[/QUOTE]
You need to enable debugging. For GCC, you add the "-g" flag. For MSVC, I think projects start with some default "debug" profile.
No, I know how to use the debugger. All I'm saying is that this specific project, the debugger doesn't help.
Are you linking to the proper SFML libraries? If you're compiling on Debug mode, remember to use sfml-system-d.lib etc.
Oh, apparently my code for assigning the render_window member of GameManager is wrong, I'll get on that.
Oh, true. You're declaring a variable on the stack then assigning its address to a member of your class. When the function returns, that variable will go away and the member will point to invalid memory.
You want to allocate on the heap and assign it to the member directly (render_window = new sf::-.....), and don't forget to delete in the destructor, unless you decide use a smart pointer.
Let us know if you still have problems after that, preferably with a reduced test case.
Oh wow. I regard myself as a intermediate programmer, but I didn't think of that at all. Thank you, gparent and Facepunch - you saved the day again.
Your debugger would've told you that :downs:
Honestly, I checked.
Sorry, you need to Log In to post a reply to this thread.