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

Is it essential to dispose of CanvasGeometry? #964

Open
SoggyBottomBoy opened this issue Oct 1, 2024 · 2 comments
Open

Is it essential to dispose of CanvasGeometry? #964

SoggyBottomBoy opened this issue Oct 1, 2024 · 2 comments

Comments

@SoggyBottomBoy
Copy link

It's not clear to me whether canvas geometry needs to be explicitly disposed of and what is cleaned up on dispose. I have a viewmodel which is responsible for creating CanvasGeometry and maintains a list of geometry to draw. This is an internal app so I am not concerned with the device changing, basically when the canvas is loaded a reference is set on the viewmodel so that I can access the device and create geometry/perform geometric operations in my viewmodel which is triggered by user actions results in a list of items to draw on the canvas. The viewmodel is then responsible for invalidating the canvas to trigger a redraw.

I have added the recommend unload behaviour to remove the canvas from the visual tree and set the Canvas to null to ensure this is garbage collected. Is this enough to ensure that any unmanaged resources are cleaned up?

@getrou
Copy link
Member

getrou commented Oct 9, 2024

That should be enough! As long as you aren't holding a reference to the CanvasGeometry, it will get garbage collected. When this happens, Win2D will release its underlying unmanaged Direct2D objects. You shouldn't need to explicitly call Dispose. Disposing this object just releases its references to the CanvasDevice or factory (which also happens when the object is garbage collected).

I just did an empirical test where I created millions of CanvasGeometries in a loop, and it didn't increase the app's memory size.

@SoggyBottomBoy
Copy link
Author

@getrou well I do hold a reference to the CanvasGeometry whilst the page is active, then when the user navigates away any CanvasGeometry will be garbage collected, along with correctly disposing of the Canvas. I expect this is fine as the CanvasGeometry won't outlive the Canvas.

But why then does it implement IDisposable if it releases its references when garbage collected? In what context would I need to ensure I call Dispose?

FYI This came about because I increased my analyzer warnings resulting in many errors for all my Win2D code.

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

No branches or pull requests

2 participants