|
-
March 20th, 2007, 09:10 PM
#1
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.
-
March 21st, 2007, 12:07 AM
#2
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.
-
March 21st, 2007, 12:15 AM
#3
Re: When do I can FreeLibrary()?
 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.
-
March 21st, 2007, 08:06 AM
#4
Re: When do I can FreeLibrary()?
humptydumpty
Is that for MFC? I'm using Win32 API.
-
March 21st, 2007, 08:42 AM
#5
Re: When do I can FreeLibrary()?
Calling it in the end of program is fine.
Regards,
Ramkrishna Pawar
-
March 30th, 2007, 08:30 AM
#6
Re: When do I can FreeLibrary()?
 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.
-
March 30th, 2007, 11:58 AM
#7
Re: When do I can FreeLibrary()?
 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
-
March 30th, 2007, 02:29 PM
#8
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.
-
March 31st, 2007, 08:46 AM
#9
Re: When do I can FreeLibrary()?
 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.
-
March 31st, 2007, 12:20 PM
#10
Re: When do I call FreeLibrary()?
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|