Thread: global and static object

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    global and static object

    Hello everyone,


    This is what mentioned in the book, ATL Internals about why Release function of CComPtr and CComQIPtr is needed for global and static variable.

    --------------------
    The destructor for global and static variable only executes after the main function exits, long after CoUninitialize runs, (That is, if it runs at all. You must link with the C++ runtime library for the constructors and destructors of global and static variable to run.) ATL itself doesn't use the C/C++ runtime library; thus, by default, ATL components don't link with the library.
    --------------------

    I do not quite understand what means "must link with the C++ runtime library for the constructors and destructors ... to run"? Could anyone help please?


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The C runtime library contains some code that looks like this (sort of, it's probably a lot more complicated, and I'm sure the names aren't right - but I'm explaining the principle, not the actual implementation):

    Code:
    int __main(...)
    {  
        int res;
        run_global_constructors();
        res = main(...);
        run_global_destructors();
        return res;
    }
    Of course, if you never link in the __main function, then you are not going to get the global constructors/destructors to run.

    static objects are no different from global objects when it comes to construction/destruction - the only difference with static objects is which bits of code can see the variable representing the object.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    So, ATL does not have any dependencies of C/C++ Runtime library? I can not imagine code which are composed of C++, which is not using C/C++ Runtime library. Any comments?

    Quote Originally Posted by matsp View Post
    The C runtime library contains some code that looks like this (sort of, it's probably a lot more complicated, and I'm sure the names aren't right - but I'm explaining the principle, not the actual implementation):

    Code:
    int __main(...)
    {  
        int res;
        run_global_constructors();
        res = main(...);
        run_global_destructors();
        return res;
    }
    Of course, if you never link in the __main function, then you are not going to get the global constructors/destructors to run.

    static objects are no different from global objects when it comes to construction/destruction - the only difference with static objects is which bits of code can see the variable representing the object.

    --
    Mats

    regards,
    George

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I have no idea of how ATL is designed - I've never worked with ATL.

    But it's perfectly possible to "use none of the C++ runtime". It just involves avoiding such functions that are part of the C++ runtime.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Let us talk about CRT only this time. :-)

    1. What functions are provided by CRT beyond STL?

    2. I think when we write console or windows app, it is linked with CRT already, and main/WinMain function is triggered by CRT start point. So, I think in this scenario, since app itself is linked with CRT, ATL used in the app should be able to utilize the CRT's function to call constructor before main and call destructor after main.

    I can not think of a scenario when we do not need CRT in a C++ app (even if ATL is not dependent on CRT directly, but ATL is used in the app which could utilize CRT constructor/destructor invocation code indirectly).

    Any comments?

    Quote Originally Posted by matsp View Post
    Well, I have no idea of how ATL is designed - I've never worked with ATL.

    But it's perfectly possible to "use none of the C++ runtime". It just involves avoiding such functions that are part of the C++ runtime.

    --
    Mats

    regards,
    George

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm sure it can be done, but you'd have to work hard at it. I think the most likely scenario of not using CRT is if you use ATL for a DLL - in which case you don't really need the CRT for much particularly if you supply your own DLLMain.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    1.

    I agree for a pure ATL DLL, no CRT and C++ runtime library are needed. But how to you use the DLL, you need to load it into an EXE, right? I think EXE must be dependent on CRT. So, the constructor and destructor can still be called.

    2.

    Since ATL is not dependent on CRT and C++ Runtime Library, I think ATL is not using any C and C++ functions and classes, like strlen and vector, right?

    Quote Originally Posted by matsp View Post
    I'm sure it can be done, but you'd have to work hard at it. I think the most likely scenario of not using CRT is if you use ATL for a DLL - in which case you don't really need the CRT for much particularly if you supply your own DLLMain.

    --
    Mats

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  2. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Replies: 2
    Last Post: 10-02-2004, 10:12 AM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM