What Herb Sutter is talking about is circular links . It promotes a layered system where resources are not passed to code that "reaches"
Layer 1 - Owns resources and Objects from level 2 (and below)
Layer 2 - Cannot have strong references to level 1 objects
This ensures that the dependency graph does not get circles. Therefore, if level 1 frees all level 2 objects, all resources will be destroyed. Why this is important is pretty simple: counting resources from the C ++ Std library cannot deal with circular references (without counting references), if obj a has a strong link to obj b and obj b has a strong link to obj a, then they never will not be released.
The ugly truth is that this is also a problem if the circle goes through several links, possibly through software modules of different authors. Without a scheme like layers, you cannot just look at the code and say: "There is no chance that this will lead to a reference to the object that I am calling." Herb Sutter offers an agreement that if you do not know the implementation, you should never call a function that could support the resource.
This does not mean that you should never do this, but if you follow a set of rules, you can check the code for each layer or even for each file without knowing the rest of the system. Otherwise, you will need to find all possible paths that the function (on_draw) could follow to see if circular deviations can occur - and if something changes in any of the possible codes, then you need to do it again!
In this context, the “callback hell” is particularly problematic, as it wraps the type system (maybe it just doesn't allow interfaces from lower levels), and the callback can do something.
If the callback does not save the resource reference, use a simple pointer instead, this clearly indicates to the caller that he does not need to worry about leaks. Not now or in the future.
source share