Win2D getting started tutorial VB Chinese version, win2dvb

Source: Internet
Author: User

Win2D getting started tutorial VB Chinese version, win2dvb

Avoid Memory leakage

This article is translated from Microsoft official documentation

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

If there is a problem with the document, you can send an Issue at https://github.com/nukepayload2/win2ddocvb, or reply directly.

When using Win2D controls in a hosted XAML application, note that the reference counting cycles before the Garbage Collector recycles these controls.

You have a problem if...

You are using Win2D from A. NET language such as VB (not c ++)

Use one of the Win2D XAML controls:

L CanvasControl

L CanvasVirtualControl

L CanvasAnimatedControl

L CanvasSwapChainPanel

L you subscribe to Win2D control events (for example, plotting, CreateResources, SizeChanged ...)

L move multiple XAML pages of your application back and forth

If all of these conditions are met, the reference counting loop will prevent the Win2D control from being reclaimed. The new Win2D Resource Allocation moves the application to a different page each time, but the old one will never be released, so the memory leaks. To avoid this problem, you must add code to explicitly break this loop.

How to fix it

Break the reference counting cycle and make your page garbage collection:

Process the Unloaded event on the Xaml page or dialog box

When uninstalling the handler, call the RemoveFromVisualTree Win2D control and release (by setting it to Nothing) any explicit reference to the Win2D control.

Sample Code:

VB

    Private Sub page_Unloaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded        Me.canvas.RemoveFromVisualTree        Me.canvas = Nothing    End Sub

 

How to test whether memory leakage

To test whether your application correctly breaks the reference loop, add the code to the terminator method of any XAML page or dialog box containing the Win2D control:

VB

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

 

Create a timer in the constructor of your application, which will enable the identified garbage collection at a fixed interval:

VB

    Dim gcTimer As New DispatcherTimer    AddHandler gcTimer.Tick, Sub() GC.Collect    gcTimer.Interval = TimeSpan.FromSeconds(1)    gcTimer.Start

 

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

About one second after the reference loop is broken, you will see "recycle canvas" in the output window"

Please note that calling GC. Collect will affect the performance, so you should delete this test code after testing.

Cruel details

Object A references B, and B also references.

A loop occurs. Or when B and B reference C, and C reference A and so on.

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

L The XAML page retains reference to all controls it contains

L controls keep references to the Event Handlers that have subscribed to them

L reference of each delegate stored in its target instance

L The event handler is usually the XAML page class of the instance method. Therefore, the reference points of the target instance are returned to the XAML page and a loop is created.

If all objects involved are implemented in. NET, this loop is not a problem because. NET garbage collection, the garbage collection algorithm can identify and recycle object groups, even if they are linked in a loop. Unlike. NET, c ++ manages the reference count of memory and cannot detect or recycle circular objects. Despite this restriction, there is no problem with c ++ applications using Win2D, because c ++ Event Handlers default to weak references rather than strong references of their target instances. Therefore, the page references the control, and the event handler delegate referenced by the control, this delegate is not referenced and returned to the page, so there is no such problem.

The problem is that when a. NET application uses the c ++ WinRT component such as Win2D:

L The XAML page is part of the application, so garbage collection is used.

L Win2D control is implemented in c ++. Therefore, reference count is used.

L The event handler delegate is part of the application, so the use of garbage collection is considered to be a strong reference to its target instance

A reference loop exists, but the Win2D object that participates in this process does not use. NET garbage collection. This means that the Garbage Collector cannot see the entire chain, so it cannot detect or recycle objects. When this happens, the application must explicitly break the loop to help. This can be done by releasing all references from the page to the control (as recommended above) or releasing from the control to possibly directing to the page (using the page uninstallation event to unsubscribe to all event handlers) all references entrusted by the event handler.

Related Article

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.