• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lride

Exiting a game by closing window.

10 posts in this topic

Can I just call exit(0) without making explicit destructor calls of objects since all dynamically allocated memory is supposed to be freed when application exits?
Or is safer to make explicit destructor calls before calling exit(0)?
0

Share this post


Link to post
Share on other sites
I have a singleton class Game and the destructor of the Game class calls delete for every game objects.
This is what my game main loop exactly looks like
[source lang="cpp"] Game game;
while(window.isOpen())
{
sf::Event event;

while(window.pollEvent(event))
{
if(event.type==sf::Event::Closed)
{
game.~Game();//so I should do this...?
exit(0);
}
}
window.clear();
game.handleEvent();
game.update();
game.draw(window);
window.display();
}[/source] Edited by lride
0

Share this post


Link to post
Share on other sites
No. The destructor should not be explicitly called.
[s]Because 'game' is on the stack, exit() when called will free everything on the stack (I think? I don't generally use exit()), and everything freed will call their own destructors.[/s]
[b][Edit:] [/b]Apparently exit() doesn't unwind the stack! [img]http://public.gamedev.net//public/style_emoticons/default/huh.png[/img] Another reason to avoid using it in your situation.

Also, 'Game' is not a singleton in the example you gave above. Just because there [i]is[/i] only one, doesn't mean it's a singleton. A singleton is something different (and something you want to avoid).

However, exit() isn't what you probably want to use there ([s]though it'd work[/s]). Instead, why not just call [b]window.close()[/b] when you get sf::Event::Closed, like the SFML documentation suggests? Alternatively, you could have your own bool instead of [b]window.isOpen()[/b], and set the bool to false when you get [b]sf::Event::Closed[/b], though the result is the same.

I like my code to exit properly out of the [b]int main()[/b] (or whatever entry point) function by a good [b]return [/b]call. Edited by Servant of the Lord
2

Share this post


Link to post
Share on other sites
If I had called the destructor explicitly, will the destructor be called again when game goes out of stack?(calling delete twice on the same memory location)
0

Share this post


Link to post
Share on other sites
Yes it would try to destruct twice and have odd side effects - hopefully crashing your program, but possibly not crashing but wrecking other parts of your progam in unpredictable ways.

But unfortunately, exit() apparently [url="http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main"]doesn't unwind the stack[/url] like I initially thought. Edited by Servant of the Lord
0

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1349727715' post='4988099']
Yes it would try to destruct twice and have odd side effects - hopefully crashing your program, but possibly not crashing but wrecking other parts of your progam in unpredictable ways.

But unfortunately, exit() apparently doesn't unwind the stack like I initially thought.
[/quote]

if it exit() doesn't unwind the stack, why would the destructor be called?
[url="http://stackoverflow.com/questions/2668075/will-exit-or-an-exception-prevent-an-end-of-scope-destructor-from-being-called"]http://stackoverflow...om-being-called[/url]

exit() is more complicated than I thought... This is what it says in the standard...
[i]Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any objects with automatic storage duration (12.4). If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior.[/i]

I have no idea what it means by
[i]If exit is called to end a program during the destruction of an object with static storage duration, the program has undefined behavior.[/i]
(say I called exit(0). Then my program has undefined behavior while returning 0. )

EDIT:
It turns out that return 0 is more favorable than exit(0)
[url="http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main"]http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main[/url] Edited by lride
0

Share this post


Link to post
Share on other sites
In general, one should only explicitly call a destructor if one also explicitly called the constructor (i.e. via placement new).

It means if you write code like this, all bets are off:
[code]
// Don't do this!

struct Bad {

~Bad() {
std::ofstream out("fin.txt");
if(!out) {
exit(1);
}
// Write buffer to out...
}

void log(const std::string &message) {
buffer.push_back(message);
}

std::vector<std::string> buffer;
};

int main()
{
static Bad bad;
bad.log("Hello, world");
// More logic
}
[/code]
The language makes no guarantees about what might happen.

Note that the intent behind the above code is another example of why immediately terminating the process can be a poor habit. Imagine a call to exit where the "main logic" is - while the OS will correctly clean up all the resources it is aware of, it cannot know that the contents of "buffer" should have been saved to a file.
2

Share this post


Link to post
Share on other sites
Note that there are some resources that the OS will not clean up, or may not clean up "properly". For Windows, an example of the former is ATOMs allocated by the [url=http://msdn.microsoft.com/en-us/library/windows/desktop/ms649060(v=vs.85).aspx]GlobalAddAtom()[/url] function, which is explicitly noted as not being cleaned up by application exit in the API documentation. An example of a clean up that might not do what you expect is are Windows [url=http://msdn.microsoft.com/en-us/library/windows/desktop/ms685129(v=vs.85).aspx]semaphore objects[/url], whose handles are automatically closed by process exit, but process exit doesn't affect semaphore count. So if you've got other processes waiting for you to finish using that semaphore, they might wait forever.
2

Share this post


Link to post
Share on other sites
[quote name='lride' timestamp='1349727950' post='4988102']
[quote name='Servant of the Lord' timestamp='1349727715' post='4988099']
Yes it would try to destruct twice and have odd side effects - hopefully crashing your program, but possibly not crashing but wrecking other parts of your progam in unpredictable ways.

But unfortunately, exit() apparently doesn't unwind the stack like I initially thought.
[/quote]

if it exit() doesn't unwind the stack, why would the destructor be called?[/quote]
It wouldn't in this case, [i]because[/i] exit() doesn't unwind the stack. If it did unwind the stack, the class would be destructed twice.

[quote]exit() is more complicated than I thought... [/quote]
Most certainly.

[quote]It turns out that return 0 is more favorable than exit(0)
[url="http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main"]http://stackoverflow...vs-exit-in-main[/url]
[/quote]
Definitely - it's always preferable to do things "the regular way" then to do things some other way that has a bunch of asterisks all over it.

Functions should return by calling 'return' or in void functions, it's also fine to just reach the end of the function though you can still call 'return' if you like). int main() is no exception. main() should exit by calling "return 0" or "return EXIT_SUCCESS" on success, or return something else on failure (preferably EXIT_FAILURE).

If you enter a function, you should exit the function properly by returning. If a problem occurs, then it's [i]permissible[/i] to exit a function through some other method (like throwing an exception, or terminating, or etc...), but each "other method" has their own select group of things you need to be aware of when using it (whether it be throwing an exception, or calling something like exit()).
Doing anything abnormal should be avoided, except when you have to when something goes wrong. Exiting a function through any method except returning is abnormal. Even when something [i]does [/i]go wrong, try to exit normally (or recover) if you can.
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0