Please help me understand this ?

FonagerFonager DKMember ✭✭


I'm struggling way to much in general with object not being released and later gc'ed, due to this. I have a simple example, could someone please tell me what I'm doing wrong ?

I have a simple ContentPage, where I instantiate and add a ToolbarItem:

using System;
using Xamarin.Forms;

namespace ScannerAppCross
    public class Test2 : ContentPage
        public Test2 ()
            var tbi = new ToolbarItem ();
            tbi.Text = "Sync";
            tbi.Icon = "ic_refresh.png";
            ToolbarItems.Add (tbi);

            Title = "Page2";
            Content = new StackLayout {
                Children = {
                    new Label { Text = "Hello ContentPage" }

The App itself opens another ContentPage "Testpage", where a button does the following:

var b = new Button { Text = "open" };
b.Clicked += (sender, e) => {

    Navigation.PushAsync (new Test2 ());


I then start the Xamarin Profiler and start with making a snapshot, after the TestPage is visible and loaded. I then push the button which pushes a new instance of Test2 onto the navigation stack. I then go back and do the same one more time. In total the Test2 page has been created and shown 2 times.

I then take another snapshot in the profiler and the total LIVE count of Test2 is 2 ??

If I uncomment this line in Test2, I will ONLY have 1 live instance of Test2:

ToolbarItems.Add (tbi);

How come ? A'm I doing something wrong ?


Sign In or Register to comment.