CLR4 托管接口导致堆损坏?
我已经使用本机 CLR 托管工作了几个星期。一开始效果很好。但后来我注意到我的应用程序中的某些内容导致堆损坏。我发现这是由CLR启动引起的。 (请参阅以下简短版本的代码。)
#pragma comment(lib, "mscoree.lib")
#include <mscoree.h>
#include <metahost.h>
#include <comdef.h>
#import "mscorlib.tlb" raw_interfaces_only \
high_property_prefixes("_get","_put","_putref") \
rename("ReportEvent", "InteropServices_ReportEvent")
using namespace mscorlib;
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr; // In fullversion used for error detection - but here unused.
PCWSTR pszVersion = L"v4.0.30319";
ICLRMetaHost* lpMetaHost = NULL;
ICLRRuntimeInfo* lpRuntimeInfo = NULL;
ICorRuntimeHost* lpRuntimeHost = NULL;
_AppDomainPtr spAppDomain = NULL;
BOOL bLoadable = false;
IUnknownPtr spAppDomainThunk = NULL;
CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID *)&lpMetaHost);
// After this line i can "late detect" 6 array bound heap corruptions in process memory.
lpMetaHost->GetRuntime(pszVersion, IID_ICLRRuntimeInfo, (LPVOID *)&lpRuntimeInfo);
lpRuntimeInfo->IsLoadable(&bLoadable);
lpRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_PPV_ARGS(&lpRuntimeHost));
lpRuntimeHost->Start();
lpRuntimeHost->GetDefaultDomain(&spAppDomainThunk);
spAppDomainThunk->QueryInterface(IID_PPV_ARGS(&spAppDomain));
spAppDomainThunk->Release();
// Now I can "late detect" up to 9 array bound heap corruptions in process memory.
return 0;
}
关于如何避免这种情况的任何想法?目前在某些情况下它仍然有效,但随着我的应用程序变得越来越大,出现错误的机会呈指数级增加。
I'm working with native CLR hosting for some weeks now. In the beginning it worked pretty well. But later on I've noticed that something in my application causes heap corruption. I've figured out that this is caused by the CLR startup. (See following short version of the code.)
#pragma comment(lib, "mscoree.lib")
#include <mscoree.h>
#include <metahost.h>
#include <comdef.h>
#import "mscorlib.tlb" raw_interfaces_only \
high_property_prefixes("_get","_put","_putref") \
rename("ReportEvent", "InteropServices_ReportEvent")
using namespace mscorlib;
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr; // In fullversion used for error detection - but here unused.
PCWSTR pszVersion = L"v4.0.30319";
ICLRMetaHost* lpMetaHost = NULL;
ICLRRuntimeInfo* lpRuntimeInfo = NULL;
ICorRuntimeHost* lpRuntimeHost = NULL;
_AppDomainPtr spAppDomain = NULL;
BOOL bLoadable = false;
IUnknownPtr spAppDomainThunk = NULL;
CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID *)&lpMetaHost);
// After this line i can "late detect" 6 array bound heap corruptions in process memory.
lpMetaHost->GetRuntime(pszVersion, IID_ICLRRuntimeInfo, (LPVOID *)&lpRuntimeInfo);
lpRuntimeInfo->IsLoadable(&bLoadable);
lpRuntimeInfo->GetInterface(CLSID_CorRuntimeHost, IID_PPV_ARGS(&lpRuntimeHost));
lpRuntimeHost->Start();
lpRuntimeHost->GetDefaultDomain(&spAppDomainThunk);
spAppDomainThunk->QueryInterface(IID_PPV_ARGS(&spAppDomain));
spAppDomainThunk->Release();
// Now I can "late detect" up to 9 array bound heap corruptions in process memory.
return 0;
}
Any ideas on how to avoid this? Currently in some cases it still works, but as my applications gets bigger the chance for an error increases exponentially.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然目视检查上面的代码并不能揭示可能导致堆损坏的原因,但请尝试 AppVerifier + Windbg 来检测它。以下是有关如何操作的一些信息
http://blogs.msdn.com/b/lagdas/archive/2008/06/24/debugging-heap-corruption-with-application-verifier-and-debugdiag.aspx。 AppVerifier 实际上可以查明堆栈(帧、调用)上损坏堆的位置。
Although visually inspecting the code above does not reveal what might be causing heap corruption, try AppVerifier + Windbg to detect it. Here is some info on how to do it
http://blogs.msdn.com/b/lagdas/archive/2008/06/24/debugging-heap-corruption-with-application-verifier-and-debugdiag.aspx. AppVerifier actually pinpoints where on the stack (frame, call) corrupts the heap.