diff --git a/src/coreclr/vm/nativeimage.cpp b/src/coreclr/vm/nativeimage.cpp index b081091f1b94e..777ab182ee8f6 100644 --- a/src/coreclr/vm/nativeimage.cpp +++ b/src/coreclr/vm/nativeimage.cpp @@ -141,14 +141,20 @@ NativeImage *NativeImage::Open( LPWSTR searchPathsConfig; IfFailThrow(CLRConfig::GetConfigValue(CLRConfig::INTERNAL_NativeImageSearchPaths, &searchPathsConfig)); - NewHolder peLoadedImage; + PEImageLayoutHolder peLoadedImage; BundleFileLocation bundleFileLocation = Bundle::ProbeAppBundle(fullPath, /*pathIsBundleRelative */ true); if (bundleFileLocation.IsValid()) { - PEImageHolder pImage = PEImage::OpenImage(fullPath, MDInternalImport_Default, bundleFileLocation); - peLoadedImage = pImage->GetOrCreateLayout(PEImageLayout::LAYOUT_MAPPED); - peLoadedImage.SuppressRelease(); + // No need to use cache for this PE image. + // Composite r2r PE image is not a part of anyone's identity. + // We only need it to obtain the native image, which will be cached at AppDomain level. + PEImageHolder pImage = PEImage::OpenImage(fullPath, MDInternalImport_NoCache, bundleFileLocation); + PEImageLayout* mapped = pImage->GetOrCreateLayout(PEImageLayout::LAYOUT_MAPPED); + // We will let pImage instance be freed after exiting this scope, but we will keep the layout, + // thus the layout needs an AddRef, or it will be gone together with pImage. + mapped->AddRef(); + peLoadedImage = mapped; } if (peLoadedImage.IsNull())