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

[runtime] Fix memory leak with BlockLiteral descriptors. Fixes #20503. #20556

Merged
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
3 changes: 2 additions & 1 deletion runtime/shared.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ -(id) initWithFunc: (init_cocoa_func *) func;
struct Block_literal *bl = (struct Block_literal *) a;
xamarin_gchandle_free (bl->global_handle);
bl->global_handle = INVALID_GCHANDLE;
if (atomic_fetch_sub (&bl->descriptor->ref_count, 1) == 0) {
// atomic_fetch_sub returns the original value before the subtraction
if (atomic_fetch_sub (&bl->descriptor->ref_count, 1) == 1) {
free (bl->descriptor); // allocated using Marshal.AllocHGlobal.
}
bl->descriptor = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/ObjCRuntime/Blocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ public void Dispose ()
// CS0420: A volatile field references will not be treated as volatile
// Documentation says: "A volatile field should not normally be passed using a ref or out parameter, since it will not be treated as volatile within the scope of the function. There are exceptions to this, such as when calling an interlocked API."
// So ignoring the warning, since it's a documented exception.
// Interlocked.Decrement returns the new value after the subtraction
var rc = Interlocked.Decrement (ref xblock_descriptor->ref_count);
#pragma warning restore 420

Expand Down
Loading