win2d Getting Started tutorial VB Chinese version-Prevent memory leaks

Source: Internet
Author: User

Avoid memory leaks

This article from Microsoft Official document translation

Http://microsoft.github.io/Win2D/html/RefCycles.htm

If there is a problem with the document, you can send Issue in Https://github.com/Nukepayload2/Win2dDocVB, or you can reply directly.

When using win2d controls in a managed XAML application, it is important to note that the garbage collector recycles these controls before their reference count loops.

You have a question, if ...

You are using win2d from a. NET language such as VB (not C + +)

You use one of the win2d XAML controls:

L Canvascontrol

L Canvasvirtualcontrol

L Canvasanimatedcontrol

L Canvasswapchainpanel

L You subscribe to win2d control events (such as drawing, createresources,sizechanged ...)

• Move your application back and forth between multiple XAML pages

If all of these conditions are met, the reference count loop will prevent the win2d control from being garbage collected. The new win2d resource is allocated every time the application moves to a different page, but the old one is never freed, so memory leaks. To avoid this problem, you must add code to explicitly break this loop.

How to fix it

Break the reference count loop and let your page be garbage collected:

Handling unloaded events for XAML pages or dialog boxes

In the unload handler, call the Removefromvisualtree win2d control and release any explicit references to the win2d control (by setting to Nothing)

Example code:

Vb

    Private Sub  as Object  as  RoutedEventArgsHandles Me. Unloaded        Me. Canvas. Removefromvisualtree        MeNothing    End Sub

How to test if memory leaks

To test whether your application correctly breaks the reference loop, add the code to the finalizer method of any XAML page or dialog box that contains the win2d control:

Vb

    Protected Overrides Sub Finalize ()        Debug. WriteLine (" recycle canvas ")        MyBase. Finalize ()    End Sub

In your application's constructor to establish a timer, it will make sure that garbage collection occurs at a fixed time interval:

Vb

    Dim  as New DispatcherTimer     AddHandler Sub  GC. Collect    TimeSpan. FromSeconds (1)    Gctimer.start

Navigate to the page and then from it to other pages.

After the reference loop is broken, you'll see the recycle canvas in the Output window for about a second.

Notice that the GC is called. Collect can affect performance, so you should remove this test code after testing

Brutal details.

Object A references B, and B also references a.

A loop occurs at this point. Or when a reference to B and B references C, and C refers to A, and so on.

When subscribing to an event's XAML control, this loop is almost inevitable:

The XAML page retains a reference to all the controls it contains

L controls keep references to event handler delegates that have subscribed to them

L Each delegate is saved to its target instance reference

The event handlers are typically XAML page classes for instance methods, so they return the target instance reference point to the XAML page, creating a loop

If all the objects involved are implemented in. NET, this loop is not an issue because . NET garbage collection, the garbage collection algorithm can identify and reclaim groups of objects, even if they are linked in a loop. With the . NET differs from the reference count of C + + management memory, which cannot detect and reclaim looping objects. Despite this limitation, There is no problem with C + + applications that use win2d because the C + + event handlers default to weak references rather than strong references to their target instances. Therefore, the page references the control, and the control references the event handler delegate, and this delegate does not refer back to the page, so there is no such problem.

The problem is when a. NET application uses C + + WinRT components such as win2d:

XAML pages are part of the application, so use garbage collection

L WIN2D Control is implemented in C + +, so reference counting is used

The event handler delegate is part of the application, so garbage collection is used to consider a strong reference to its target instance

A reference loop is present, but the Win2d object that participates in this does not use. NET garbage collection. This means that the garbage collector is unable to see the entire chain, so it cannot detect or reclaim objects. When this happens, the application must help by explicitly breaking the loop. This can be done by releasing all references from the page to the control (as suggested above) or by releasing all the references from control to an event handler delegate that might point to the page (using the page unload event to unsubscribe all event handlers).

win2d Getting Started tutorial VB Chinese version-Prevent memory leaks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.