FreeLibrary()是否釋放模快

ggg82 2005-05-15 03:04:36
我尝试用findfirst查找所有的dll文件,然后調用loadlibrary加載,嘗試调用dllregisterservers注册该dll文件,然后用freelibrary释放,可是,程序运行所用内存逐渐变大,最后出错,为什么会出现这样的情况,怎样解决
...全文
248 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggg82 2005-05-30
  • 打赏
  • 举报
回复
不好意思,我换了系统,在2000下,没有发现这个问题,上次是xp系统,也许是xp系统有问题
ggg82 2005-05-29
  • 打赏
  • 举报
回复
Regsvr32每次只对一个dll进行注册呀,当然没有这样的问题了,我的问题是程序最后崩溃了,因为内存使用量太大了,产生了运行时错误呀
san_huo 2005-05-29
  • 打赏
  • 举报
回复
进程是一回事,系统是另一回事。这里讲的是dll映射进进程地址空间的情况。其实这个讨论可能已经跑题了,不过还是贴一下msdn的相关描述吧:

Each process maintains a reference count for each loaded library module. This reference count is incremented each time LoadLibrary is called and is decremented each time FreeLibrary is called. A DLL module loaded at process initialization due to load-time dynamic linking has a reference count of one. This count is incremented if the same module is loaded by a call to LoadLibrary.

Before unmapping a library module, the system enables the DLL to detach from the process by calling the DLL's DllMain function, if it has one, with the DLL_PROCESS_DETACH value. Doing so gives the DLL an opportunity to clean up resources allocated on behalf of the current process. After the entry-point function returns, the library module is removed from the address space of the current process.

xwzxwz 2005-05-29
  • 打赏
  • 举报
回复
我回去做做实验吧

在网吧没环境
xwzxwz 2005-05-28
  • 打赏
  • 举报
回复
san_huo(煽风点火):

FreeLibrary调用之后在此进程中卸载DLL,DLL计数变为0后从内存中卸载
xwzxwz 2005-05-28
  • 打赏
  • 举报
回复
没做实验

有个建议,自己用IDA看看RegSvr32.exe怎么做的
ggg82 2005-05-27
  • 打赏
  • 举报
回复
还有更好的解释吗
san_huo 2005-05-24
  • 打赏
  • 举报
回复
FreeLibrary只是减少DLL的引用技术,不会立即卸载DLL,除非DLL计数变为0
ggg82 2005-05-23
  • 打赏
  • 举报
回复
int main(int argc,char *argv[])
{
WIN32_FIND_DATA wfd;
HANDLE hFind;
if(argc<2)
return 0;
hFind=FindFirstFile(argv[1],&wfd);
if(-1!=(int)hFind)
{
CoInitialize(NULL);
do
{
HMODULE hLib;
printf("RegFile %s:",wfd.cFileName);
hLib=LoadLibrary(wfd.cFileName);
if(hLib)
{
long (*DllReg)();
DllReg=(long (*)())GetProcAddress(hLib,"DllRegisterServer");
if(DllReg)
{
if(S_OK==DllReg())
printf("OK\n");
else
printf("Fail\n");
}
else
printf("Bad Reg File\n");
FreeLibrary(hLib);
}
else
printf("LoadLibrary Fail\n");
//尝试注册该文件*/
}while(FindNextFile(hFind,&wfd));
FindClose(hFind);
CoUninitialize();
}
else
printf("error\n");
return 0;
}
//编译程序,使用参数c:\windows\system32\*.dll调用程序,会发现,程序占用内存逐渐增大,最后产生运行错误被终止
sboom 2005-05-22
  • 打赏
  • 举报
回复
应该使你程序有错吧,而且freelibrary不一定释放模块,有其他进程在用的时候就不释放,只是对你释放而已。
jerry 2005-05-22
  • 打赏
  • 举报
回复
我想你这段代码有问题,不妨帖出来看看
ggg82 2005-05-22
  • 打赏
  • 举报
回复
可是,我在循环外部进行初始化,循环还未结束,怎么释放呢
oyljerry 2005-05-19
  • 打赏
  • 举报
回复
CoInitialize初始化了也要释放CoUnInitialize
ggg82 2005-05-19
  • 打赏
  • 举报
回复
已经初始化了,可是好象并不释放内存
jerry 2005-05-15
  • 打赏
  • 举报
回复
要先初始化COM:

CoInitialize(NULL);
CodeProject-Jerry 2005-05-15
  • 打赏
  • 举报
回复
你不用 loadlibrary 啊

直接调用 RegSvr32.exe 去注册啊

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧