Description
Description
ClassLoader::ComputeLoaderModuleWorker
uses a LoaderAllocator creation number to determine the latest LoaderAllocator to place a class in. However the method does not account the number of the type definition itself when calculating the latest number and this can lead into a situation when the generic instance is put into an older LoaderAllocator.
E.g. if we have type ClassA
from AssemblyLoadContext A
with number 2 and type ClassB
from AssemblyLoadContext B
with number 5 the resulting loader allocator for the type ClassB
would correspond to AssemblyLoadContext A
with creation number 2. If ClassB
contains a static, such static would be then placed under the LoaderAllocator A
, preventing AssemblyLoadContext B
from unloading unless A
is unloaded.
Reproduction Steps
- Create 2 classes in 2 different assemblies
Assembly A
public class ClassA
{
}
Assembly B
public class ClassB<T>
{
public static T instance = new T();
}
- Load
A.dll
into a collectible AssemblyLoadContextA
. - Load
B.dll
into a collectible AssemblyLoadContextB
. - Instantiate type
ClassB<ClassA>
. - Ensure instance is not referenced and unload AssemblyLoadContext
B
.
Expected behavior
AssemblyLoadContext B
is unloaded successfully
Actual behavior
AssemblyLoadContext B
is not unloaded
Regression?
No
Known Workarounds
No response
Configuration
No response
Other information
The following change seem to fix the issue - Unity-Technologies#279
Metadata
Metadata
Assignees
Type
Projects
Status