gpt4 book ai didi

c++ - CLR4 托管接口(interface)导致堆损坏?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:00:58 25 4
gpt4 key购买 nike

我已经使用 native 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;
}

Rational Purify Exception

关于如何避免这种情况的任何想法?目前在某些情况下它仍然有效,但随着我的应用程序越来越大,出错的可能性呈指数级增加。

最佳答案

虽然目视检查上面的代码并没有揭示可能导致堆损坏的原因,但请尝试 AppVerifier + Windbg 来检测它。这是有关如何操作的一些信息 http://blogs.msdn.com/b/lagdas/archive/2008/06/24/debugging-heap-corruption-with-application-verifier-and-debugdiag.aspx . AppVerifier 实际上会查明堆栈(帧、调用)中损坏堆的位置。

关于c++ - CLR4 托管接口(interface)导致堆损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7075504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com