-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Valgrind reports Native AOT shared library memory leaks #81008
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas Issue DetailsDescriptionWhen calling a C# function from C/C++ via the NativeAOT shared library, valgrind reports multiple memory leaks. At first, I though it was become I was linking to the shared library like a normal C library and that was causing the leak. So I changed it and used Reproduction StepsClass.cs using System.Runtime.InteropServices;
namespace AOT;
public static class Interface {
[UnmanagedCallersOnly( EntryPoint = "add" )]
public static int Add( int a, int b ) {
return a + b;
}
} AOT.csproj <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>libaot</AssemblyName>
<PublishAot>true</PublishAot>
<NativeLib>shared</NativeLib>
</PropertyGroup>
</Project> main_extern.cpp #include <iostream>
extern "C" {
int add( int a, int b );
}
void run( ) {
auto result = add( 10, 20 );
std::cout << result << std::endl;
}
int main( ) {
run( );
} main_dlload.cpp #include <iostream>
#include <dlfcn.h>
#define symload dlsym
typedef int(*addFunc)(int, int);
void run( ) {
void* handle = dlopen( "./publish/libaot.so", RTLD_LAZY );
addFunc add = (addFunc)symload( handle, "add" );
auto result = add( 10, 20 );
std::cout << result << std::endl;
dlclose( handle );
}
int main( ) {
run( );
}
Expected behaviorI expected valgrind to report no memory leaks. Actual behaviorValgrind reported reachable leaks and possibly lost leaks: (Note: I edited the output to remove personal info)
Regression?No response Known WorkaroundsNo response Configuration.NET SDK: Runtime Environment: Host: .NET SDKs installed: .NET runtimes installed: Other architectures found: Environment variables: global.json file: Other informationOn another attempt, I exposed the [UnmanagedCallersOnly( EntryPoint = "gc_collect" )]
public static void GCCollect( ) {
GC.Collect( );
}
|
I'm not sure how to interpret this - do you see more and more memory getting leaked as you call the NativeAOT is not unloadable (#64629) - once the library is loaded into the process, it's expected to stay there forever. So it won't try to do cleanup because it's designed for the ultimate cleanup - process termination. The "leaked" amount should remain more or less constant. |
Ok. Did not know it was unloadable. No extra leaks occur on other calls. Then would it be safe to ignore theses in valgrind via a suppression file?
|
Yes, I think so |
As long as we don't support unloading, this is expected, no need to track it with an issue. |
Description
When calling a C# function from C/C++ via the NativeAOT shared library, valgrind reports multiple memory leaks.
At first, I though it was become I was linking to the shared library like a normal C library and that was causing the leak. So I changed it and used
dlsym
as suggested in the sample project, but that didn't work either.Reproduction Steps
Class.cs
AOT.csproj
main_extern.cpp
main_dlload.cpp
dotnet publish -o publish -r linux-x64
g++ main_dlload.cpp -o main_dlload
g++ main_extern.cpp publish/libaot.so -o main_extern
valgrind ./main_dlload
orvalgrind ./main_extern
Expected behavior
I expected valgrind to report no memory leaks.
Actual behavior
Valgrind reported reachable leaks and possibly lost leaks:
(Note: I edited the output to remove personal info)
Regression?
No response
Known Workarounds
No response
Configuration
.NET SDK:
Version: 7.0.102
Commit: 4bbdd14480
Runtime Environment:
OS Name: ubuntu
OS Version: 22.04
OS Platform: Linux
RID: ubuntu.22.04-x64
Base Path: /usr/share/dotnet/sdk/7.0.102/
Host:
Version: 7.0.2
Architecture: x64
Commit: d037e07
.NET SDKs installed:
6.0.405 [/usr/share/dotnet/sdk]
7.0.102 [/usr/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 7.0.2 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.13 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
None
Environment variables:
DOTNET_ROOT [/usr/share/dotnet/]
global.json file:
Not found
Other information
On another attempt, I exposed the
GC.Collect( )
function to C++ hoping that forcing the garbage collector to cleanup before disposing of the library handle would remove the leaks, but that did nothing.The text was updated successfully, but these errors were encountered: