locked
Life cycle of the App object of a Windows Store application

    Question

  • I'm learning Windows Store application development in Visual Studio 2012. The main entry of a window store application is generated by the dev tool automatically in App.g.hpp:

    #ifndef DISABLE_XAML_GENERATED_MAIN
    int main(::Platform::Array<::Platform::String^>^ args)
    {
        (void)args; // Unused parameter
        Windows::UI::Xaml::Application::Start(ref new Windows::UI::Xaml::ApplicationInitializationCallback(
            [](Windows::UI::Xaml::ApplicationInitializationCallbackParams^ p) {
                (void)p; // Unused parameter
                auto app = ref new ::Tester::App();
            }));
    }
    #endif

    I have a question about the line "auto app = ref new ::Tester::App();". The scope of "app" is the body of the Lamda. So the object pointed by "app" should be destructed on the return of the Lamda expression. But this should not be the case. How is the instantiated App object kept alive? Anything magic here, or I've misunderstood something?

    Thanks,

    -Oscar

     

    • Moved by Mike Danes Friday, December 07, 2012 2:16 PM Store apps related (From:Visual C++ General)
    Friday, December 07, 2012 2:02 PM

Answers

  • The missing piece is that the created App object is reference counted and will live until it is unreferenced. The reference from the app variable goes away, but the constructor creates other references which keep it alive.

    --Rob

    Friday, December 07, 2012 11:40 PM
    Owner