Skip to content

[Android] Fix for Resize method returns an image that has already been disposed #29964

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

SyedAbdulAzeemSF4852
Copy link
Contributor

@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 commented Jun 12, 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!

Issue Details

  • In GraphicsView, the Resize method returns an image that has already been disposed.

Root Cause

  • The Bitmap property returned the internal _bitmap instance directly, which would be disposed during the Dispose() call if disposeBitmap was set to true, potentially leading to access of a disposed object.

Description of Change

  • Updated the Bitmap property to return a new bitmap instance using Bitmap.CreateBitmap, ensuring that the original bitmap is not reused after disposal.

Reference

public override ICanvas Canvas => _canvas;
public UIImage UIImage => UIImage.FromImage(_bitmapContext.ToImage());
public PlatformImage PlatformImage => new PlatformImage(UIImage);
public CGImage CGImage => _bitmapContext.ToImage();
public override IImage Image => PlatformImage;

Issues Fixed

Fixes #29961

Validated the behaviour in the following platforms

  • Windows
  • Android
  • iOS
  • Mac

Output

Before After
Before.mov
After.mov

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jun 12, 2025
Copy link
Contributor

Hey there @@SyedAbdulAzeemSF4852! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Jun 12, 2025
@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 marked this pull request as ready for review June 16, 2025 12:54
@Copilot Copilot AI review requested due to automatic review settings June 16, 2025 12:54
@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 requested a review from a team as a code owner June 16, 2025 12:54
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures the Android Resize method returns a valid, non-disposed image by cloning the internal bitmap when disposal is requested.

  • Clone the internal bitmap in PlatformBitmapExportContext.Bitmap instead of returning the original.
  • Added a UI test in TestCases.Shared.Tests to verify the Resize method.
  • Implemented a host app page in TestCases.HostApp and embedded the sample image resource.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
src/Graphics/src/Graphics/Platforms/Android/PlatformBitmapExportContext.cs Updated the Bitmap getter to return a copied instance when _disposeBitmap is true
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue29961.cs Added a UI test to verify the resized image is not disposed
src/Controls/tests/TestCases.HostApp/Issues/Issue29961.cs Created a host app page and button for exercising the resize behavior
src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj Embedded the royals.png image used by the host app test
Comments suppressed due to low confidence (1)

src/Graphics/src/Graphics/Platforms/Android/PlatformBitmapExportContext.cs:40

  • [nitpick] Cloning the bitmap on every access can be expensive. Consider caching the copied instance or providing an explicit CloneBitmap() method to avoid repeated allocations when Bitmap is accessed multiple times.
public Bitmap Bitmap => _disposeBitmap ? _bitmap.Copy(_bitmap.GetConfig(), false) : _bitmap;

{
Button button = new Button
{
AutomationId = $"Issue21886_1ResizeBtn",
Copy link
Preview

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

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

The AutomationId is reused from Issue21886 and may conflict with other tests. Please update it to a unique identifier (e.g., Issue29961_ResizeBtn) to ensure uniqueness.

Copilot uses AI. Check for mistakes.

@@ -0,0 +1,27 @@
#if TEST_FAILS_ON_WINDOWS // Issue Link - https://github.com/dotnet/maui/issues/16767
Copy link
Preview

Copilot AI Jun 16, 2025

Choose a reason for hiding this comment

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

The test is conditionally compiled only for Windows (TEST_FAILS_ON_WINDOWS), which prevents it from executing on Android. Remove or adjust the compilation symbol so the test runs on all relevant platforms.

Copilot uses AI. Check for mistakes.

@PureWeen PureWeen added this to the .NET 9 SR10 milestone Jun 26, 2025
{
Button button = new Button
{
AutomationId = $"Issue21886_1ResizeBtn",
Copy link
Contributor

Choose a reason for hiding this comment

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

This AutomationId looks like duplicated, could you use the issue identifier number?
Issue29961_ResizeBtn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz , Updated the AutomationId based on the suggestion.

public void VerifyResizeMethodReturnsValidImage()
{
App.WaitForElement("ConvertedImageStatusLabel");
App.Tap("Issue21886_1ResizeBtn");
Copy link
Contributor

Choose a reason for hiding this comment

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

Issue29961_ResizeBtn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jsuarezruiz , Updated the AutomationId based on the suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

In GraphicsView, the Resize method returns an image that has already been disposed
3 participants