AFAIK.
The leak detector of a class FOO is based on a local static variable and is built at the first instantiation of a FOO object. So according to C++ standard every static object constructed before will be released after (in reverse order). That's why you get false-positives. When the leak detector is destroyed (as Foo objects could still be owned by those oldest static objects) leaks are reported.
Note that "constructed" means totally constructed. If a static A calls a local static B in its constructor, B will be considered constructed before A. So B will be released after A. Note that "local static" means "function static" and "non-local" static means "global static".
I'm not english speaking native. So please correct me if something is wrong.
Various approaches could be used to turn off LEAK_DETECTOR hell, but it really depends on situations.
0. Do NOT use LEAK_DETECTOR in that case.
1. You could use an Initialize/Shutdown instead (as JUCE does) to explicitly manage order of construction.
2. SharedResourcePointer example in the documentation isn't good enough?
3. Example:
- Static class FOO is constructed.
- Static class BAR is constructed (with LEAK_DETECTOR).
- A BAR instance is added to (owned by) the FOO container.
- Shutdown.
- BAR's LEAK_DETECTOR destructor is called : Warning! There's still a BAR instance in FOO!
...
Solution: Instantiate a dummy BAR before FOO. It can be in the FOO constructor ;-)
class Foo {
public:
Foo( ) { Bar("Dummy"); }
};
class Bar {
public:
Bar(const String& hello) { DBG(hello); }
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Bar)
};
static Foo container;
...
Now:
- Static class FOO constructor is called.
- The static BAR's LEAK_DETECTOR is constructed.
- Static class FOO is constructed.
- A BAR instance is added to (owned by) the FOO container.
- Shutdown.
- FOO destructor is called.
- BAR's LEAK_DETECTOR destructor is called.