Skip to content

Commit

Permalink
Merge pull request #6939 from agocke/no-unloading
Browse files Browse the repository at this point in the history
Add comments about .NET unload
  • Loading branch information
agocke authored Jun 6, 2024
2 parents 1d30d94 + 2eaa262 commit d2053c0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions core/hosting/src/NativeHost/nativehost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ namespace
return false;

// Load hostfxr and get desired exports
// NOTE: The .NET Runtime does not support unloading any of its native libraries. Running
// dlclose/FreeLibrary on any .NET libraries produces undefined behavior.
void *lib = load_library(buffer);
init_for_cmd_line_fptr = (hostfxr_initialize_for_dotnet_command_line_fn)get_export(lib, "hostfxr_initialize_for_dotnet_command_line");
init_for_config_fptr = (hostfxr_initialize_for_runtime_config_fn)get_export(lib, "hostfxr_initialize_for_runtime_config");
Expand Down
3 changes: 1 addition & 2 deletions core/nativeaot/NativeLibrary/LoadLibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ int callSumFunc(char *path, char *funcName, int firstInt, int secondInt)

int result = MyImport(firstInt, secondInt);

// CoreRT libraries do not support unloading
// See https://github.com/dotnet/corert/issues/7887
// NOTE: Native AOT libraries do not support unloading
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions core/nativeaot/NativeLibrary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ The last thing to do is to actually call the method we have imported.
int result = MyImport(5,3);
```

Note that the .NET Runtime does not support unloading. Once a handle to the shared library is created, the library cannot be closed with `dlclose/FreeLibrary`.

## Exporting methods

For a C# method in the native library to be consumable by external programs, it has to be explicitly exported using the `[UnmanagedCallersOnly]` attribute.
Expand Down

0 comments on commit d2053c0

Please sign in to comment.