summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2017-01-03 05:02:10 -0700
committerRui Marinho <me@ruimarinho.net>2017-01-03 12:02:10 +0000
commit58909e205a6b10fd1ed834c0ea5a37950504d035 (patch)
tree3d7d29a6b6fdb48af4792d3e0f30319a39531482 /Xamarin.Forms.Controls.Issues
parentf003cfd3886adb85cd6dd10e8083bc82abb68234 (diff)
downloadxamarin-forms-58909e205a6b10fd1ed834c0ea5a37950504d035.tar.gz
xamarin-forms-58909e205a6b10fd1ed834c0ea5a37950504d035.tar.bz2
xamarin-forms-58909e205a6b10fd1ed834c0ea5a37950504d035.zip
Allow subscriber to be collected if MessagingCenter is the only reference to it (#617)
* Repro * Make messaging center callbacks weak references * Preserve attribute * Fix test method name * Watch for collection of actual delegate target instead of wrapper delegate * Preserve the original platform instance when changing main page * Better tests for lambda situations * Update tests, make callback target a weakreference if it's the subscriber * Ensure old Platform MessagingCenter subs are gone before creating new Platform
Diffstat (limited to 'Xamarin.Forms.Controls.Issues')
-rw-r--r--Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45926.cs111
-rw-r--r--Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems1
2 files changed, 112 insertions, 0 deletions
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45926.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45926.cs
new file mode 100644
index 00000000..0006d2e8
--- /dev/null
+++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45926.cs
@@ -0,0 +1,111 @@
+using System;
+using System.Threading;
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+
+ [Preserve(AllMembers = true)]
+ [Issue(IssueTracker.Bugzilla, 45926, "MessagingCenter prevents subscriber from being collected", PlatformAffected.All)]
+ public class Bugzilla45926 : TestNavigationPage
+ {
+ protected override void Init()
+ {
+ Button createPage, sendMessage, doGC;
+
+ Label instanceCount = new Label();
+ Label messageCount = new Label();
+
+ instanceCount.Text = $"Instances: {_45926SecondPage.InstanceCounter.ToString()}";
+ messageCount.Text = $"Messages: {_45926SecondPage.MessageCounter.ToString()}";
+
+ var content = new ContentPage {
+ Title = "Test",
+ Content = new StackLayout {
+ VerticalOptions = LayoutOptions.Center,
+ Children = {
+ (createPage = new Button { Text = "New Page" }),
+ (sendMessage = new Button { Text = "Send Message" }),
+ (doGC = new Button { Text = "Do GC" }),
+ instanceCount, messageCount
+ }
+ }
+ };
+
+ createPage.Clicked += (s, e) => PushAsync (new _45926SecondPage ());
+ sendMessage.Clicked += (s, e) =>
+ {
+ MessagingCenter.Send (this, "Test");
+ };
+
+ doGC.Clicked += (sender, e) => {
+ GC.Collect ();
+ GC.WaitForPendingFinalizers();
+ instanceCount.Text = $"Instances: {_45926SecondPage.InstanceCounter.ToString()}";
+ messageCount.Text = $"Messages: {_45926SecondPage.MessageCounter.ToString()}";
+ };
+
+ PushAsync(content);
+ }
+
+#if UITEST
+ [Test]
+ public void Issue45926Test ()
+ {
+ RunningApp.WaitForElement (q => q.Marked ("New Page"));
+
+ RunningApp.Tap (q => q.Marked ("New Page"));
+ RunningApp.Back();
+ RunningApp.Tap(q => q.Marked("Do GC"));
+ RunningApp.Tap(q => q.Marked("Send Message"));
+ RunningApp.Tap(q => q.Marked("Do GC"));
+
+ RunningApp.WaitForElement (q => q.Marked ("Instances: 0"));
+ RunningApp.WaitForElement (q => q.Marked ("Messages: 0"));
+ }
+#endif
+ }
+
+ [Preserve(AllMembers = true)]
+ public class _45926SecondPage : ContentPage
+ {
+ public static int InstanceCounter = 0;
+ public static int MessageCounter = 0;
+
+ public _45926SecondPage ()
+ {
+ Interlocked.Increment(ref InstanceCounter);
+
+ Content = new Label {
+ HorizontalOptions = LayoutOptions.Center,
+ VerticalOptions = LayoutOptions.Center,
+ Text = "Second Page #" + (InstanceCounter)
+ };
+
+ MessagingCenter.Subscribe<Bugzilla45926> (this, "Test", OnMessage);
+ }
+
+ protected override void OnDisappearing ()
+ {
+ base.OnDisappearing ();
+ }
+
+ void OnMessage (Bugzilla45926 app)
+ {
+ System.Diagnostics.Debug.WriteLine ("Got Test message!");
+ Interlocked.Increment(ref MessageCounter);
+ }
+
+ ~_45926SecondPage ()
+ {
+ Interlocked.Decrement(ref InstanceCounter);
+ System.Diagnostics.Debug.WriteLine ("~SecondPage: {0}", GetHashCode ());
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
index e0af0ca2..36ed04cb 100644
--- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
+++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
@@ -479,6 +479,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla36802.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla35736.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla48158.cs" />
+ <Compile Include="$(MSBuildThisFileDirectory)Bugzilla45926.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">