How can I manage heap order objects?

Let's say I have 2 singleton allocated per heap for which delete is never called. Let me call them A and B. Is there a way to make sure that B will be the first to be destroyed?

I assume that the platform may make a difference for this: Visual Studio 2005 Professional, Visual C ++. Everything was built with help cl.

+3
source share
3 answers

If they are a bunch allocated to regular pointers, and if deletion is never called on these pointers, they will never be destroyed, so the order of destruction will be debatable.

, , :

static std::auto_ptr <AType> a( new AType );  // destroyed second
static std::auto_ptr <BType> b( new BType );  // destroyed first

: -)

+5

atexit, LIFO.

+4

:

:
: ++

:

, singeltons (, ), .

To ensure that B is destroyed before A, then B must be created before A. To do this, simply make constructor A to get an instance of B.

See here: C ++ Singleton design pattern for a lot of links on singeltons.

+1
source

Source: https://habr.com/ru/post/1731378/


All Articles