Skip to content

Commit

Permalink
.NET: Fix AssemblyLoadContext logic
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Apr 2, 2024
1 parent 5f90e3f commit 397ae50
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions csharp-api/REFrameworkNET/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ namespace REFrameworkNET {
}

auto self = System::Reflection::Assembly::LoadFrom(System::Reflection::Assembly::GetExecutingAssembly()->Location);
s_default_context = gcnew PluginLoadContext();

for each (System::String^ file in files) {
System::Console::WriteLine(file);
Expand Down Expand Up @@ -338,8 +339,6 @@ namespace REFrameworkNET {
continue;
}

s_default_context = gcnew PluginLoadContext();

auto assem = s_default_context->LoadFromStream(gcnew System::IO::MemoryStream(bytecode));
//auto assem = System::Reflection::Assembly::Load(bytecode);

Expand Down Expand Up @@ -393,7 +392,7 @@ namespace REFrameworkNET {
}

void PluginManager::UnloadDynamicAssemblies() {
if (PluginManager::s_dynamic_assemblies == nullptr) {
if (PluginManager::s_dynamic_assemblies == nullptr || PluginManager::s_dynamic_assemblies->Count == 0) {
REFrameworkNET::API::LogInfo("No dynamic assemblies to unload");
return;
}
Expand Down Expand Up @@ -432,26 +431,28 @@ namespace REFrameworkNET {
s_dynamic_assemblies->Clear();

// make weak ref to default context
System::WeakReference^ weakRef = gcnew System::WeakReference(s_default_context);
PluginManager::s_default_context->Unload();
PluginManager::s_default_context = nullptr;

bool unloaded = false;

for (int i = 0; i < 10; i++) {
if (weakRef->IsAlive) {
System::GC::Collect();
System::GC::WaitForPendingFinalizers();
System::Threading::Thread::Sleep(10);
} else {
unloaded = true;
System::Console::WriteLine("Successfully unloaded default context");
break;
if (s_dynamic_assemblies != nullptr) {
System::WeakReference^ weakRef = gcnew System::WeakReference(s_default_context);
PluginManager::s_default_context->Unload();
PluginManager::s_default_context = nullptr;

bool unloaded = false;

for (int i = 0; i < 10; i++) {
if (weakRef->IsAlive) {
System::GC::Collect();
System::GC::WaitForPendingFinalizers();
System::Threading::Thread::Sleep(10);
} else {
unloaded = true;
System::Console::WriteLine("Successfully unloaded default context");
break;
}
}
}

if (!unloaded) {
System::Console::WriteLine("Failed to unload default context");
if (!unloaded) {
System::Console::WriteLine("Failed to unload default context");
}
}
}
}

0 comments on commit 397ae50

Please sign in to comment.