Skip to content

Commit

Permalink
Fix refcount when a composite r2r image is loaded from a bundle (#61066)
Browse files Browse the repository at this point in the history
* Fix refcount when a composite r2r image is loaded from a bundle

* PR feedback - better comments.
  • Loading branch information
VSadov committed Nov 3, 2021
1 parent 24e7a4a commit 12222d4
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/coreclr/vm/nativeimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,20 @@ NativeImage *NativeImage::Open(
LPWSTR searchPathsConfig;
IfFailThrow(CLRConfig::GetConfigValue(CLRConfig::INTERNAL_NativeImageSearchPaths, &searchPathsConfig));

NewHolder<PEImageLayout> 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())
Expand Down

0 comments on commit 12222d4

Please sign in to comment.