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

Author Topic: Question about abort(), assert(), exit(), what is best for shared_ptr?  (Read 4275 times)

0 Members and 1 Guest are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Hello, in my game I have this memory model by which I compose a grid and store the data structure Tile dynamically with shared_ptr in a 2d vector.

There are times that in my game there are certain routines that have to be done and if that routine fails. My game exits. I use exit() function to do this.

How can I guarantee that all resources are freed and released?

I haven't tried to read the C++11 manual for this, and even if I do, I might not understand it very well.
Which one should I use best for my situation?

Thanks

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Question about abort(), assert(), exit(), what is best for shared_ptr?
« Reply #1 on: August 14, 2013, 07:41:12 pm »
exit() will likely work best, as it tries to exit as cleanly as possible. But I doubt you HAVE to abort the entire program. That seems... extremely draconian. Are you sure there isn't another way to handle any errors?
I use the latest build of SFML2

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Question about abort(), assert(), exit(), what is best for shared_ptr?
« Reply #2 on: August 14, 2013, 07:44:07 pm »
The only other way I think of is to make sure all the routine finishes all. I have to guarantee that. I was just wondering. I just getting a little bit of conservative on this part of my code. Well, I think this is something I have to think about ... again.



binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Question about abort(), assert(), exit(), what is best for shared_ptr?
« Reply #3 on: August 14, 2013, 07:44:35 pm »
From C++ standard:
Quote
[[noreturn]] void abort(void) noexcept;
The function abort() has additional behavior in this International Standard:
— The program is terminated without executing destructors for objects of automatic, thread, or static storage duration and without calling functions passed to atexit() (3.6.3).
Quote
The function exit() has additional behavior in this International Standard:
— First, objects with thread storage duration and associated with the current thread are destroyed. Next, objects with static storage duration are destroyed and functions registered by calling atexit are called. See 3.6.3 for the order of destructions and calls. (Automatic objects are not destroyed as a result of calling exit().) If control leaves a registered function called by exit because the function does not provide a handler for a thrown exception, std::terminate() shall be called (15.5.1).
— Next, all open C streams (as mediated by the function signatures declared in <cstdio>) with unwritten buffered data are flushed, all open C streams are closed, and all files created by calling tmpfile() are removed.
— Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
From C standard:
Quote
Synopsis
#include <assert.h>
void assert(scalar expression);
Description
The assert macro puts diagnostic tests into programs; it expands to a void expression. When it is executed, if expression (which shall have a scalar type) is false (that is, compares equal to 0), the assert macro writes information about the particular call that failed (including the text of the argument, the name of the source file, the source line number, and the name of the enclosing function — the latter are respectively the values of the preprocessing macros __FILE__ and __LINE__ and of the identifier __func__) on the standard error stream in an implementation-defined format. It then calls the abort function.
Returns
The assert macro returns no value.
Since none of these functions destroys automatic objects, std::shared_ptrs will always leak when terminating your application using them. Exiting your application using these functions is not a healthy practice anyway, and I would recommend looking for another way to terminate your application cleanly instead of relying on exit().
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Question about abort(), assert(), exit(), what is best for shared_ptr?
« Reply #4 on: August 14, 2013, 08:27:20 pm »
binary, I was under the impression that exit() calls the destructors to all currently created objects. In fact, that seems to be the first bullet point for exit(). And I was also under the impression that std::shared_ptr cleans up the dynamically allocated memory in the destructor when it's the last shared pointer to that memory. So shouldn't exit() clean up the pointers?
I use the latest build of SFML2

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Question about abort(), assert(), exit(), what is best for shared_ptr?
« Reply #5 on: August 14, 2013, 08:41:30 pm »
Quote
Automatic objects are not destroyed as a result of calling exit().
Automatic meaning automatic lifetime i.e. all variables declared within a block of code and end up on the stack as opposed to free store. Because local variables aren't destroyed, any std::shared_ptr that exists as a local variable or part of another object that exists solely on the stack will leak since their destructors will not be called by exit(). The only objects that are destroyed are those with thread storage duration i.e. sf::ThreadLocalPtr and objects with static storage duration i.e. static variables and globals. exit() and abort() can be seen as anti-RAII and are just relics from C times that really have nothing to look for in modern C++ code.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Question about abort(), assert(), exit(), what is best for shared_ptr?
« Reply #6 on: August 15, 2013, 12:22:10 pm »
Hello, in my game I have this memory model by which I compose a grid and store the data structure Tile dynamically with shared_ptr in a 2d vector.
Why do you need std::shared_ptr<T>, as opposed to T or std::unique_ptr<T>?

There are times that in my game there are certain routines that have to be done and if that routine fails. My game exits. I use exit() function to do this.

How can I guarantee that all resources are freed and released?
By not using std::exit() (remember, all C++ standard library functions are in namespace std, even if compilers aren't very strict here).

You have to propagate the error back to main() properly. Either by multiple return paths or -- in exceptional cases -- using exceptions.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything