T
T
t0xic2013-04-11 20:15:38
OOP
t0xic, 2013-04-11 20:15:38

Removing singletons

Good evening,
at work there was a task to correctly remove singletons (there are more than 40-50 of them).
and different types (static and dynamic).
But the problem is that due to their large number, it is not clear when exactly they can be deleted, and due to the fact that they are incorrectly deleted or not deleted at all, at the end of the application, the application rarely but falls on removal.

Question: how can this be properly organized?
I myself think it is correct to implement the Destroy () methods for deleting memory behind a singleton and everything else that is allocated dynamically.
The Terminate() method for clearing sheets and any objects inside.
Perhaps it makes sense to have some kind of manager over them in order to delete them correctly ...

Nobody faced a similar problem? Maybe someone came across and will outline an idea or tell you in which direction to move.

Thanks for any post.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
yse, 2013-04-11
@t0xic

Try to dig in the direction of using the atexit() function - this function registers user-defined functions that are called when the application exits. In general, if there are a lot of singletons, look at the implementation of Alexandrescu in the Loki library

M
Mephi1984, 2013-04-11
@Mephi1984

If something like a static initialization order fiasco occurs during deletion (errors occur due to the indefinite order of deletion of singletons), then you can put all singletons in one object, like fields - then they will be deleted in the reverse order of declaration.
The most convenient way to use RAII is to allocate memory in the constructor, and delete it in the destructor, then Terminate is not needed either. Of course, this does not always work out, but in the main it is.
Dynamically allocated memory is best stored in shared_ptr, which implements RAII.

I
isvirin, 2013-04-11
@isvirin

How you generally organize garbage collection? How is the order in which objects are removed determined?
If you are not using smart pointers, I highly recommend using them. Only when using, be attentive to another possible problem - cyclic references, otherwise nothing will be deleted at all;)
As for singletons, distribute them as smart pointers. Store them all in one registry (as already advised above), so that at the time of deletion, they are all reset and released, so to speak, free swimming. They themselves will be removed in the correct order through the use of smart pointers.
Any Terminate (or Shutdown) is needed in order to break possible cyclic links, of which the singleton itself is a member.
In general, when developing at least some complex programs (40-50 singletons is already a lot), I recommend using a dedicated kernel entity, which will create and initialize all instances (especially singletons) at startup, and “release” them when they are all deleted . It is important what exactly will be “released” and not deleted, because between in the slightest degree complex system there are always connections that the simple logic of “deletion in reverse order” will not break correctly. Link counting is up to you.
We tested this approach on very, very large systems (thousands of objects, hundreds of singletons) - everything turns on and off correctly :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question