CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2007
    Posts
    19

    When do I call FreeLibrary()?

    When I create a Rich Edit Control I load the DLL by calling

    LoadLibrary("RichEd20.dll")

    Do I have to call FreeLibrary() at some point? If so, when do I call it? Do I call it when the program ends? Thanks.
    Last edited by vironic; March 21st, 2007 at 08:05 AM.

  2. #2
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: When do I can FreeLibrary()?

    You can put it after the message pump.

    Code:
    // other WinMain code...
    
    // Message pump
    while(GetMessage(...))
    {
        TranslateMessage(...);
        DispatchMessage(...);
        // etc...
    }
    
    FreeLibrary(...);
    
    // End of WinMain...
    return static_cast<int>(msg.wParam);
    I think, I'm not 100% sure if thats the best place for it, but it should work.

    Regards
    -NotSoSuperHero.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  3. #3
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: When do I can FreeLibrary()?

    Quote Originally Posted by vironic
    When I create a Rich Edit Control I load the DLL by calling

    LoadLibrary("RichEd20.dll")

    Do I have to call FreeLibrary() at some point? If so, when do I call it? Do I call it when the program ends? Thanks.
    call it in your CWinApp::ExitInstance() Function.Which is the right place to free the loaded library
    Thanx
    Last edited by humptydumpty; March 21st, 2007 at 12:17 AM.

  4. #4
    Join Date
    Jan 2007
    Posts
    19

    Re: When do I can FreeLibrary()?

    humptydumpty

    Is that for MFC? I'm using Win32 API.

  5. #5
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: When do I can FreeLibrary()?

    Calling it in the end of program is fine.
    Regards,
    Ramkrishna Pawar

  6. #6
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    Re: When do I can FreeLibrary()?

    Quote Originally Posted by Krishnaa
    Calling it in the end of program is fine.
    Wasnt it like this that an NT system takes care of all open handles when a process exits ? If so, then there is no need to do a cleanup. I mean it can be a side effect of a system trying to reclaim "orphan" resources back, but in practice this gives programmer an ability to get away without cleanup, which takes time and program size. After all if a system has a map of all handles of a process, then theoretically a process dont have to cleanup..
    Last edited by Amn; March 30th, 2007 at 08:32 AM.

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: When do I can FreeLibrary()?

    Quote Originally Posted by Amn
    Wasnt it like this that an NT system takes care of all open handles when a process exits ? If so, then there is no need to do a cleanup. I mean it can be a side effect of a system trying to reclaim "orphan" resources back, but in practice this gives programmer an ability to get away without cleanup, which takes time and program size. After all if a system has a map of all handles of a process, then theoretically a process dont have to cleanup..
    I've heard this argument a couple of times. While this might work on Windows systems, it might not work on other systems (especially microcontrollers).

    Personally, I think this is a bad habit to get into.

    My $0.02...

    Viggy

  8. #8
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: When do I can FreeLibrary()?

    Stylistically, I always try to add creation and cleanup code at the same time. The moment I add some creation code, I look for a complementary subtask/area where the cleanup fits in.

    Programmatically, it is a good practise too. Say, the code that is now in the exe, is moved to another component , say DLL , and is invoked that way. In this case, since the cleanup code is there, the cleanup is not postponed to the end of the program, but to the unloading of the component.

    In the OPs case, it might not matter since the process is exiting anyway. It is like a RegisterClass call. How many times have we seen a Win32 program calling RegisterClass but also calling UnregisterClass before exiting I haven't seen one so far

    But , it is good practise to always cleanup at the right time. As MrViggy says, it is a bad habit to get into.

  9. #9
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    Re: When do I can FreeLibrary()?

    Quote Originally Posted by MrViggy
    I've heard this argument a couple of times. While this might work on Windows systems, it might not work on other systems (especially microcontrollers).

    Personally, I think this is a bad habit to get into.

    My $0.02...

    Viggy
    Well, i was not trying to advocate the approach really. I just wanted to point out the particular strategy in use by NT systems.

    I dont want to get off-topic to start another discussion here, but i think it is a fairly smart strategy where handles are owned and maintained by OS, so the program only needs to "borrow" them, and when a "process in memory" implies that when the process is unloaded all references to handles are taken care of - i.e. handles are returned to the system. my two cents :-)

    I am in no way encouraging the practice, especially when going cross-platform for obvious reasons you stated.

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: When do I call FreeLibrary()?

    Quote Originally Posted by vironic
    When I create a Rich Edit Control I load the DLL by calling

    LoadLibrary("RichEd20.dll")

    Do I have to call FreeLibrary() at some point? If so, when do I call it? Do I call it when the program ends? Thanks.
    I would like to question the need to LoadLibrary() for Rich Edit controls.
    How do you use this library?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured