• Sergey Makarenko's avatar
    Register singleton's destruction using std::atexit · 3f4a8ae0
    Sergey Makarenko authored
    Summary:
    scheduleDestroyInstances function is called from createInstance function when
    new instance of specific singleton type is created thus marking a point in
    static objects order  when all singltons will be destructed.
    This does not work well for situations when singleton is loaded as part of
    python extension which is a shared object. In this case static objects of this
    shared object would be constructed first and singleton from this extension will
    be created after that. Since destruction order is reversed and all singletons
    will be destructed together, static objects of python extension will be
    destroyed before singleton from this extension. Which leads to heap-after-free
    ASAN exceptions.
    In other words, lifetime of all folly singletons is aligned to the lifetime of the
    first created folly singleton.
    Using std::atexit to register singleton's destruction from singleton
    constructor will align  lifetime of singletons to the most recent singleton
    which matters for python extensions and does not matter for other use cases.
    
    Reviewed By: andriigrynenko
    
    Differential Revision: D6705644
    
    fbshipit-source-id: 5c933886ceae649e3c75f8e7e7936d5a7ed04539
    3f4a8ae0
Singleton.cpp 11.7 KB