Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments about .NET unload #6939

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add a sentence at the end of this section in the README too? https://github.com/dotnet/samples/tree/main/core/nativeaot/NativeLibrary#loading-shared-libraries-from-c-and-importing-methods?

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
Loading