CLR4 托管接口导致堆损坏?

发布于 2024-11-29 12:31:28 字数 1702 浏览 1 评论 0原文

我已经使用本机 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

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

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;
}

Rational Purify Exception

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-12-06 12:31:28

虽然目视检查上面的代码并不能揭示可能导致堆损坏的原因,但请尝试 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文