关于CRT的DLL版本的始终还没搞清楚的问题,再来问下!

zyTech001 2009-01-04 09:17:42
对于CRT的静态链接库的版本就很容易理解了,就是想像把所有CRT的代码都嵌入到应用程序中,然后调用_mainCRTStartup(),在
其中也会调用_heap_init来初始化_crtheap堆,从代码看malloc new等都是从这个堆上分配内存的,看上去很顺理成章。

但是CRT的动态链接库版本就真的让我很恼。
如果我的exe文件是使用CRT的动态链接库版本,而且也没有使用其他的DLL的话,那么其入口应该是使用crtexe.c文件中的_main
CRTStartup()函数吧,而它并没有初始化_crtheap,那么_crtheap是在什么时候初始化的呢?
...全文
1068 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
ckt 2009-01-04
  • 打赏
  • 举报
回复
在都使用CRT动态链接版本的前提下,为什么dll中分配的内存可以在exe中释放?

你使用的是CRT DLL,那么你在exe中调用的new,delete,malloc,free等函数的实现代码都在dll中.
所以实际上你的分配,释放操作应该都属于在dll中的操作.
zyTech001 2009-01-04
  • 打赏
  • 举报
回复
这个选项设置我清楚
只不过请你再解释一下:在都使用CRT动态链接版本的前提下,为什么dll中分配的内存可以在exe中释放?我的认识就是dll
中没有进行_crtheap的初始化吧。可是从_CRTDLL_INIT中看,就算是在DLL应用程序中它还是进行了heap的初始化啊,
症结就在这里,只要这里弄清楚了就好了
ckt 2009-01-04
  • 打赏
  • 举报
回复
标准程序库有关的选项: /ML /MLd /MT /MTd /MD /MDd. 这些选项告诉编译器使用什么版本的C标准程序库.
/ML(缺省选项)对应单线程静态版的标准程序库(libc.lib).
/MT对应多线程静态版标准库(libcmt.lib), 此时编译器会自动定义_MT宏.
/MD对应多线程DLL版(导入库msvcrt.lib, DLL是msvcrt.dll), 编译器自动定义_MT和_DLL两个宏.
后面加d的选项都会让编译器自动多定义一个_DEBUG宏, 表示要使用对应标准库的调试版.
因此/MLd对应调试版单线程静态标准库(libcd.lib),/MTd对应调试版多线程静态标准库(libcmtd.lib),
/MDd对应调试版多线程DLL标准库(导入库msvcrtd.lib, DLL是msvcrtd.dll).

zyTech001 2009-01-04
  • 打赏
  • 举报
回复
可不可以这样理解_CRTDLL_INIT函数是MSVCRT*.DLL这个DLL的入口函数,而DLL应用程序的入口函数则根据其编译选项的不同
会选择crtdll.c中的动态版本或者dllcrt0.c中的静态版本
ckt 2009-01-04
  • 打赏
  • 举报
回复
to Chiyer:
确实如此.
原本一直认为_DllMainCRTStartup是最先被调用的.
星羽 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 ckt1120 的回复:]
to Chiyer:
我用的是VC6.0 _DllMainCRTStartupdll里调用的就是_CRT_INIT
[/Quote]

这个在vs2005里也是,但是在调用 _DllMainCRTStartupdll 之前,会先调用

_CRTDLL_INIT

_CRTDLL_INIT 在 crtlib.c里
ckt 2009-01-04
  • 打赏
  • 举报
回复
to Chiyer:
我用的是VC6.0 _DllMainCRTStartupdll里调用的就是_CRT_INIT
ckt 2009-01-04
  • 打赏
  • 举报
回复
个人看法:
每个dll(包括crt dll),被导入到每个模块(exe或dll)时,
dll的数据成员在该模块中都会有自己的副本.
所以dll可以同时被两个不同的exe所使用,而dll的数据变量不会冲突.
当crt dll导入多次时crt dll中的_crtheap也是有多个.


回到你原始的问题,你在exe无法看到_heap_init的调用.
是_heap_init在crt dll中调用了,因为你使用分配释放函数是在dll中实现的.
星羽 2009-01-04
  • 打赏
  • 举报
回复

BOOL __cdecl
__CRTDLL_INIT(
HANDLE hDllHandle,
DWORD dwReason,
LPVOID lpreserved
)
{
unsigned int osplatform = 0;
unsigned int winver = 0;
unsigned int winmajor = 0;
unsigned int winminor = 0;
unsigned int osver = 0;

if ( dwReason == DLL_PROCESS_ATTACH ) {
OSVERSIONINFOA *posvi;
/*
* Dynamically allocate the OSVERSIONINFOA buffer, so we avoid
* triggering the /GS buffer overrun detection. That can't be
* used here, since the guard cookie isn't available until we
* initialize it from here!
*/
posvi = (OSVERSIONINFOA *)HeapAlloc(GetProcessHeap(), 0, sizeof(OSVERSIONINFOA));
if (!posvi)
return FALSE;

/*
* Get the full Win32 version.
*/
posvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
if ( !GetVersionExA(posvi) ) {
HeapFree(GetProcessHeap(), 0, posvi);
return FALSE;
}

osplatform = posvi->dwPlatformId;
winmajor = posvi->dwMajorVersion;
winminor = posvi->dwMinorVersion;

/*
* The somewhat bizarre calculations of _osver and _winver are
* required for backward compatibility (used to use GetVersion)
*/
osver = (posvi->dwBuildNumber) & 0x07fff;
HeapFree(GetProcessHeap(), 0, posvi);
posvi = NULL;
if ( osplatform != VER_PLATFORM_WIN32_NT )
osver |= 0x08000;
winver = (winmajor << 8) + winminor;

_set_osplatform(osplatform);
_set_winver(winver);
_set_winmajor(winmajor);
_set_winminor(winminor);
_set_osver(osver);

#if defined (_M_IX86) && defined (_SYSCRT)

/*
* Make sure we are NOT running on Win32s
*/
if ( osplatform == VER_PLATFORM_WIN32s ) {
__crtMessageBoxA("MSVCRT.DLL for Win32\n\nError: MSVCRT.DLL is not compatible with Win32s.",
"Microsoft Visual C++ Runtime Library",
MB_OK|MB_ICONHAND|MB_SETFOREGROUND|MB_TASKMODAL);
return FALSE;
}

#endif /* defined (_M_IX86) && defined (_SYSCRT) */

if ( !_heap_init(1) ) /* initialize heap */
/*
* The heap cannot be initialized, return failure to the
* loader.
*/
return FALSE;

if(!_mtinit()) /* initialize multi-thread */
{
/*
* If the DLL load is going to fail, we must clean up
* all resources that have already been allocated.
*/
_heap_term(); /* heap is now invalid! */

return FALSE; /* fail DLL load on failure */
}

if (_ioinit() < 0) { /* inherit file info */
/* Clean up already-allocated resources */
/* free TLS index, call _mtdeletelocks() */
_mtterm();

_heap_term(); /* heap is now invalid! */

return FALSE; /* fail DLL load on failure */
}

_aenvptr = (char *)__crtGetEnvironmentStringsA();

_acmdln = (char *)GetCommandLineA();
_wcmdln = (wchar_t *)__crtGetCommandLineW();

...
...


return TRUE;
}
星羽 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ckt1120 的回复:]
引用 2 楼 zache 的回复:
我看的是vc6的代码,和这个差很多啊!
那如果一个DLL应用程序也是使用CRT的动态链接库版本呢,是不是也是要先运行__CRTDLL_INT函数后才会接着运行
__DllMainCRTStartup()函数呢?


CRT_INIT是在_DllMainCRTStartup中被调用
每个动态库都有自己的堆(题外话,在某个动态库中进行的堆分配操作,也要在该动态库中释放,否则会报错)
你可以在_CRT_INIT处下个断点看下
C/C++ codeBOOL WINAPI _DllMainC…
[/Quote]

对与dll crt heap的初始化是在 _CRTDLL_INIT 而不是 CRT_INIT,你自己看看代码就知道了
星羽 2009-01-04
  • 打赏
  • 举报
回复
上面说错了。。。是进程
星羽 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zache 的回复:]
更加迷惑的是vc6中我找到的_CRTDLL_INIT的代码只是简单调用了_heap_init,似乎没有代码来判断这个_crtheap是否
已经被初始化了呀

我是说看上去似乎每个dll都有自己的_crtheap
[/Quote]

每个线程有自己的heap
zyTech001 2009-01-04
  • 打赏
  • 举报
回复
更加迷惑的是vc6中我找到的_CRTDLL_INIT的代码只是简单调用了_heap_init,似乎没有代码来判断这个_crtheap是否
已经被初始化了呀

我是说看上去似乎每个dll都有自己的_crtheap
zyTech001 2009-01-04
  • 打赏
  • 举报
回复
5楼的话让我十分迷惑啊

从msdn上的文档上看到,如果exe和dll都是使用CRT的动态链接库版本的话,那么在dll中分配的内存可以在exe中释放。
也就是说如果都使用CRT DLL版本的话应该只有一个_crtheap才对呀是吧

可是又说每个dll都有一个自己的堆,这到底是什么意思?
更加迷惑的是vc6中我找到的_CRTDLL_INIT的代码只是简单调用了_heap_init,似乎没有代码来判断这个_crtheap是否
已经被初始化了呀

请指教谢谢
ckt 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zache 的回复:]
我看的是vc6的代码,和这个差很多啊!
那如果一个DLL应用程序也是使用CRT的动态链接库版本呢,是不是也是要先运行__CRTDLL_INT函数后才会接着运行
__DllMainCRTStartup()函数呢?
[/Quote]

CRT_INIT是在_DllMainCRTStartup中被调用
每个动态库都有自己的堆(题外话,在某个动态库中进行的堆分配操作,也要在该动态库中释放,否则会报错)
你可以在_CRT_INIT处下个断点看下

BOOL WINAPI _DllMainCRTStartup(
HANDLE hDllHandle,
DWORD dwReason,
LPVOID lpreserved
)
{
BOOL retcode = TRUE;

/*
* If this is a process detach notification, check that there has
* been a prior process attach notification.
*/
if ( (dwReason == DLL_PROCESS_DETACH) && (__proc_attached == 0) )
return FALSE;

if ( dwReason == DLL_PROCESS_ATTACH || dwReason == DLL_THREAD_ATTACH )
{
if ( _pRawDllMain )
retcode = (*_pRawDllMain)(hDllHandle, dwReason, lpreserved);

if ( retcode )
retcode = _CRT_INIT(hDllHandle, dwReason, lpreserved);

if ( !retcode )
return FALSE;
}

retcode = DllMain(hDllHandle, dwReason, lpreserved);


if ( (dwReason == DLL_PROCESS_ATTACH) && !retcode )
/*
* The user's DllMain routine returned failure, the C runtime
* needs to be cleaned up. Do this by calling _CRT_INIT again,
* this time imitating DLL_PROCESS_DETACH. Note this will also
* clear the __proc_attached flag so the cleanup will not be
* repeated upon receiving the real process detach notification.
*/
_CRT_INIT(hDllHandle, DLL_PROCESS_DETACH, lpreserved);

if ( (dwReason == DLL_PROCESS_DETACH) ||
(dwReason == DLL_THREAD_DETACH) )
{
if ( _CRT_INIT(hDllHandle, dwReason, lpreserved) == FALSE )
retcode = FALSE ;

if ( retcode && _pRawDllMain )
retcode = (*_pRawDllMain)(hDllHandle, dwReason, lpreserved);
}

return retcode ;
}
星羽 2009-01-04
  • 打赏
  • 举报
回复

dll crt 版本,都会先调用

BOOL WINAPI
_CRTDLL_INIT(
HANDLE hDllHandle,
DWORD dwReason,
LPVOID lpreserved
)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
/*
* The /GS security cookie must be initialized before any exception
* handling targetting the current image is registered. No function
* using exception handling can be called in the current image until
* after __security_init_cookie has been called.
*/
__security_init_cookie();
}

return __CRTDLL_INIT(hDllHandle, dwReason, lpreserved);
}

然后在 __CRTDLL_INIT(hDllHandle, dwReason, lpreserved); 会做一系列初始化




exe ,和dll都一样


星羽 2009-01-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zache 的回复:]
我看的是vc6的代码,和这个差很多啊!
那如果一个DLL应用程序也是使用CRT的动态链接库版本呢,是不是也是要先运行__CRTDLL_INT函数后才会接着运行
__DllMainCRTStartup()函数呢?
[/Quote]

是的
zyTech001 2009-01-04
  • 打赏
  • 举报
回复
我看的是vc6的代码,和这个差很多啊!
那如果一个DLL应用程序也是使用CRT的动态链接库版本呢,是不是也是要先运行__CRTDLL_INT函数后才会接着运行
__DllMainCRTStartup()函数呢?
星羽 2009-01-04
  • 打赏
  • 举报
回复

dll版本在调用 _mainCRTStartup() 前就始化了_crtheap,
在crtlib.c的这个函数里

BOOL WINAPI
_CRTDLL_INIT(
HANDLE hDllHandle,
DWORD dwReason,
LPVOID lpreserved
)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
/*
* The /GS security cookie must be initialized before any exception
* handling targetting the current image is registered. No function
* using exception handling can be called in the current image until
* after __security_init_cookie has been called.
*/
__security_init_cookie();
}

return __CRTDLL_INIT(hDllHandle, dwReason, lpreserved);
}

你可以在这个函数里下一个断点,你会发现它先被调用才到 __tmainCRTStartup





zyTech001 2009-01-04
  • 打赏
  • 举报
回复
受教了,我是没掌握方法,要是学会了怎样设置断点就可以自己查看了
加载更多回复(5)

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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