Thread: CoInitialize[ex]/CoUnitialize calls

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    CoInitialize[ex]/CoUnitialize calls

    For an application that makes little use of COM, where is the most effecient place to make the CoInitialize/Unitialize calls?

    Currently I use COM only briefly in a modal dialog and CoInitialize it in the WM_CREATE message, CoUnitializing it on WM_NCDESTROY. However the literature is a bit ambiguous as to if this is a good strategy. Some sources say this is an effective way to reduce memory usage by COM (versus placing the calls at program init/end), others say there may be time required between the init call and when it is actually initialized, making calls in between them prone to error.

    So where is a good place for these calls to both limit resources used and still ensure COM is intialized properly?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Personally, I would use the program entry/exit strategy to reduce the chance of any latency issues cropping up. If you're using C++ you could even wrap it up into a class and have everything happen automagically.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    The difference in memory between the two methods is 10 K before a COM call and about 1 MB after, I'll keep trying other placements and see what happens.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by @nthony View Post
    For an application that makes little use of COM, where is the most effecient place to make the CoInitialize/Unitialize calls?

    Currently I use COM only briefly in a modal dialog and CoInitialize it in the WM_CREATE message, CoUnitializing it on WM_NCDESTROY. However the literature is a bit ambiguous as to if this is a good strategy. Some sources say this is an effective way to reduce memory usage by COM (versus placing the calls at program init/end), others say there may be time required between the init call and when it is actually initialized, making calls in between them prone to error.

    So where is a good place for these calls to both limit resources used and still ensure COM is intialized properly?
    I have had issues with COM not freeing resources quickly enough, causing the next call to COM to fail as out of memory (but in XPe with EWF).

    Depends on how often / quickly the dlg can be called and how quickly COM will be required after create.
    It may be that a small Sleep() will fix any issue (while waiting for COM to finish)
    or
    CoInitialize may have to be called at app create (not dlg create).

    IMO only call CoInitialize once for the app / threads lifetime.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    On Win7 once you initialize COM, there's 261KB of memory allocated that you'll never get back, no matter how many calls to CoUninitialize() you make. This "leak" is present on earlier versions of Windows too but the amount differs. I realize that's nothing nowadays, just thought it might interest the OP as I remember a previous thread on the tightness of MS's code.

    Oh and the app that produced that trace was just:
    Code:
    int main()
    {
        CoInitialize(NULL);
        CoUninitialize();
        ExitProcess(0);
    }
    Last edited by adeyblue; 07-24-2009 at 11:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Unicode v ANSI Calls
    By Davros in forum Windows Programming
    Replies: 3
    Last Post: 04-18-2006, 09:35 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Which I/O calls do you use the most?
    By _Elixia_ in forum Tech Board
    Replies: 10
    Last Post: 07-11-2003, 11:58 AM
  5. C/C++ Memory Calls!
    By Joda in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2002, 04:50 PM