Skip to content

Fix RealParent garbage collection warning to reduce noise in production apps #30291

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 28, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

This PR fixes the noisy "The RealParent on Microsoft.Maui.Controls.Shapes.RoundRectangle has been Garbage Collected" warnings that have been flooding production app logs since .NET MAUI SR6, particularly on iOS.

Problem

Users were experiencing repeated warning messages like:

Microsoft.Maui.Controls.Element: Warning: The RealParent on Microsoft.Maui.Controls.Shapes.RoundRectangle has been Garbage Collected. This should never happen. Please log a bug: https://github.com/dotnet/maui

These warnings would appear hundreds of times in production apps, making logs difficult to read and potentially impacting performance.

Root Cause

The issue occurs when:

  1. A parent-child relationship is established using weak references in the RealParent property
  2. The parent gets garbage collected while the child element still exists
  3. The child tries to access RealParent, finds the weak reference target is null
  4. A warning is logged every time this happens, with no cleanup of the invalid reference

Solution

Enhanced defensive handling in Element.RealParent getter:

  1. Clear invalid weak references: When a weak reference target is garbage collected, immediately set _realParent = null to prevent repeated warnings on subsequent accesses

  2. Conditional logging: Only log warnings in DEBUG builds using #if DEBUG to reduce noise in production while still helping developers catch issues during development

  3. Preserve behavior: Maintain the same return value (null) and overall functionality to ensure backward compatibility

Changes Made

Core Fix (Element.cs)

// Before: Would log warning on every access after GC
if (_realParent.TryGetTarget(out var parent))
{
    return parent;
}
else
{
    Application.Current?
        .FindMauiContext()?
        .CreateLogger<Element>()?
        .LogWarning($"The RealParent on {this} has been Garbage Collected...");
}

// After: Clear reference and only log in debug builds  
if (_realParent.TryGetTarget(out var parent))
{
    return parent;
}
else
{
    // Clear the weak reference since the target has been garbage collected
    _realParent = null;

    // Only log warning in debug builds to reduce noise in production
#if DEBUG
    Application.Current?
        .FindMauiContext()?
        .CreateLogger<Element>()?
        .LogWarning($"The RealParent on {this} has been Garbage Collected...");
#endif
}

Comprehensive Tests (RealParentGCTests.cs)

Added 4 focused test cases covering:

  • GC scenario handling without exceptions
  • Valid reference behavior
  • Null parent handling
  • Multiple access scenarios after GC

Testing

  • ✅ All existing 473 Element-related tests pass
  • ✅ All existing ParentLifeCycle tests pass
  • ✅ New comprehensive test suite covers edge cases
  • ✅ Build succeeds with no breaking changes

Impact

For Users:

  • ✅ Eliminates noisy warning spam in production app logs
  • ✅ Maintains full backward compatibility
  • ✅ Improves app performance by avoiding repeated logging

For Developers:

  • ✅ Still logs warnings in DEBUG builds to catch real issues
  • ✅ Better weak reference lifecycle management
  • ✅ Comprehensive test coverage for future maintenance

This is a minimal, surgical fix that addresses the immediate pain point while preserving the underlying diagnostic capability for development scenarios.

Fixes #23050.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Microsoft.Maui.Controls.Element: Warning: The RealParent on Microsoft.Maui.Controls.Shapes.RoundRectangle has been Garbage Collected. This should never happen. Please log a bug: https://github.com/dotnet/maui Fix RealParent garbage collection warning to reduce noise in production apps Jun 28, 2025
@Copilot Copilot AI requested a review from PureWeen June 28, 2025 02:51
Copilot finished work on behalf of PureWeen June 28, 2025 02:51
Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

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

Include the fixes from this PR
#30156

But also improve the changes where you see fit to

_realParent = null;

// Only log warning in debug builds to reduce noise in production
#if DEBUG
Copy link
Member

Choose a reason for hiding this comment

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

Remove this, we should log a warning in release builds as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the #if DEBUG conditional compilation. Warnings now log in both debug and release builds. (commit 1c53139)

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 28, 2025

Include the fixes from this PR
#30156

...

Incorporated the fixes from PR #30156 including the GetRealParent method refactor, conditional warning logging parameter, and improved SetParent logic. Also removed the #if DEBUG conditional as requested. (commit 1c53139)

@Copilot Copilot AI requested a review from PureWeen June 28, 2025 12:33
Copilot finished work on behalf of PureWeen June 28, 2025 12:33
@PureWeen
Copy link
Member

Can you update the copilot instructions to indicate that if there's a PR already opened for an issue you are assigned to that you didn't create, that you will review the changes in that other PR and give your opinion on it? If you feel like the solution in the other PR is valid then use that PR to influence what you do but then see where you can improve and add tests

@@ -397,7 +405,7 @@ void SetParent(Element value)

if (_parentOverride == null)
{
OnParentChangingCore(Parent, value);
OnParentChangingCore(ParentOverride ?? realParent, value);
Copy link
Member

Choose a reason for hiding this comment

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

Does this cause duplication of logic? Since we're also doing this inside Parent.get is there a way to consolidate that logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right. I've consolidated the logic by changing SetParent to use the Parent property instead of calling GetRealParent(false) separately. This eliminates the duplication and ensures consistent behavior between Parent.get and SetParent. (commit f8a79e9)

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 28, 2025

Can you update the copilot instructions to indicate that if there's a PR already opened for an issue you are assigned to that you didn't create, that you will review the changes in that other PR and g...

Updated the copilot instructions to include guidance for AI agents on handling existing PRs for assigned issues. The new section covers reviewing existing solutions, using valid work as foundation, and identifying improvement opportunities rather than duplicating efforts. (commit f5545ed)

@Copilot Copilot AI requested a review from PureWeen June 28, 2025 17:04
Copilot finished work on behalf of PureWeen June 28, 2025 17:04
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copilot finished work on behalf of PureWeen June 28, 2025 17:13
@PureWeen
Copy link
Member

I want to test out the updated copilot instructions

Can you start this PR completely over as if you were just assigned this issue?
Except use the updated copilot instructions that are part of this PR

…ase on PR #30156 with improvements

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 28, 2025

I want to test out the updated copilot instructions

Can you start this PR completely over as if you were just assigned this issue?
...

Started completely fresh following the updated copilot instructions! Successfully reviewed PR #30156, validated its approach, used it as foundation, and enhanced it with comprehensive improvements including weak reference cleanup and robust test coverage. (commit 845fb06)

Copilot finished work on behalf of PureWeen June 28, 2025 17:54
@PureWeen
Copy link
Member

This is looking good

Can you add some more notes in copilot instructions just indicating that you shouldn't try too hard to base your work off existing pr

It's ok to completely abandon the already open pr if you aren't confident enough of that approach. If you do abandon it make sure to add notes why you abandoned that approach. Always include a summary of what you think about any other open prs for the issue you are working on

…ity and abandonment guidance

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 28, 2025

This is looking good

Can you add some more notes in copilot instructions just indicating that you shouldn't try too hard to base your work off existing pr ...

Enhanced the copilot instructions with additional flexibility guidance: added notes about not forcing existing PR approaches, making it clear that abandoning existing PRs is acceptable when not confident, requiring documentation of abandonment reasoning, and always including summaries of other open PRs. (commit a73d1ca)

Copilot finished work on behalf of PureWeen June 28, 2025 18:30
@PureWeen
Copy link
Member

I want to test out the updated copilot instructions

Can you start this PR completely over as if you were just assigned this issue?
Except use the updated copilot instructions that are part of this PR

…0156 with enhancements

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 28, 2025

I want to test out the updated copilot instructions

Can you start this PR completely over as if you were just assigned this issue?
...

Started completely fresh following the updated copilot instructions! I thoroughly reviewed PR #30156, found their approach solid, and used it as my foundation while adding critical enhancements like weak reference cleanup. All 473 Element tests pass with the improved implementation. (commit a8e73ba)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Microsoft.Maui.Controls.Element: Warning: The RealParent on Microsoft.Maui.Controls.Shapes.RoundRectangle has been Garbage Collected. This should never happen. Please log a bug: https://github.com/dotnet/maui
2 participants