2

私はネイティブ 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 例外

これを回避する方法についてのアイデアはありますか?現在、場合によってはまだ機能しますが、アプリケーションが大きくなるにつれて、エラーの可能性が指数関数的に増加します。

4

1 に答える 1

0

上記のコードを視覚的に調べても、ヒープの破損の原因を特定することはできませんが、AppVerifier + Windbg を試して検出してください。http://blogs.msdn.com/b/lagdas/archive/2008/06/24/debugging-heap-corruption-with-application-verifier-and-debugdiag.aspxの方法に関する情報を次に示します 。AppVerifier は、スタック (フレーム、呼び出し) のどこでヒープが破損したかを実際に特定します。

于 2012-02-29T06:42:01.950 に答える