summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-08-30 17:57:55 (GMT)
committerJason Smith <jason.smith@xamarin.com>2016-08-30 17:57:55 (GMT)
commit974705b9a920e71ecaa9f5e0d58cf6d80a1d71cf (patch)
treecaa8ac25f603ee796f455846e615011afb9b702a /Xamarin.Forms.Platform.Android
parenta0dae2d111a5fc59a272f5b8a27885a54dcf6f32 (diff)
downloadxamarin-forms-974705b9a920e71ecaa9f5e0d58cf6d80a1d71cf.zip
xamarin-forms-974705b9a920e71ecaa9f5e0d58cf6d80a1d71cf.tar.gz
xamarin-forms-974705b9a920e71ecaa9f5e0d58cf6d80a1d71cf.tar.bz2
Fix memory leak caused by BaseCellView and RendererHolder (#311)
Diffstat (limited to 'Xamarin.Forms.Platform.Android')
-rw-r--r--Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs8
-rw-r--r--Xamarin.Forms.Platform.Android/Cells/CellRenderer.cs31
2 files changed, 32 insertions, 7 deletions
diff --git a/Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs b/Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs
index 74c56db..c4d10cc 100644
--- a/Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs
+++ b/Xamarin.Forms.Platform.Android/Cells/BaseCellView.cs
@@ -18,7 +18,7 @@ namespace Xamarin.Forms.Platform.Android
public const double DefaultMinHeight = 44;
readonly Color _androidDefaultTextColor;
- readonly Cell _cell;
+ Cell _cell;
readonly TextView _detailText;
readonly ImageView _imageView;
readonly TextView _mainText;
@@ -179,6 +179,12 @@ namespace Xamarin.Forms.Platform.Android
LayoutParameters = new LayoutParams(ViewGroup.LayoutParams.MatchParent, (int)(height == -1 ? ViewGroup.LayoutParams.WrapContent : height));
}
+ protected override void OnDetachedFromWindow()
+ {
+ base.OnDetachedFromWindow();
+ _cell = null;
+ }
+
async void UpdateBitmap(ImageSource source, ImageSource previousSource = null)
{
if (Equals(source, previousSource))
diff --git a/Xamarin.Forms.Platform.Android/Cells/CellRenderer.cs b/Xamarin.Forms.Platform.Android/Cells/CellRenderer.cs
index e62a8d4..fd7cd89 100644
--- a/Xamarin.Forms.Platform.Android/Cells/CellRenderer.cs
+++ b/Xamarin.Forms.Platform.Android/Cells/CellRenderer.cs
@@ -24,7 +24,7 @@ namespace Xamarin.Forms.Platform.Android
public AView GetCell(Cell item, AView convertView, ViewGroup parent, Context context)
{
Performance.Start();
-
+
Cell = item;
Cell.PropertyChanged -= PropertyChangedHandler;
@@ -33,14 +33,18 @@ namespace Xamarin.Forms.Platform.Android
if (convertView != null)
{
Object tag = convertView.Tag;
- var renderHolder = tag as RendererHolder;
- if (renderHolder != null)
+ CellRenderer renderer = (tag as RendererHolder)?.Renderer;
+
+ Cell oldCell = renderer?.Cell;
+
+ if (oldCell != null)
{
- Cell oldCell = renderHolder.Renderer.Cell;
((ICellController)oldCell).SendDisappearing();
if (Cell != oldCell)
+ {
SetRenderer(oldCell, null);
+ }
}
}
@@ -50,7 +54,7 @@ namespace Xamarin.Forms.Platform.Android
var holder = view.Tag as RendererHolder;
if (holder == null)
- view.Tag = new RendererHolder { Renderer = this };
+ view.Tag = new RendererHolder(this);
else
holder.Renderer = this;
@@ -126,7 +130,22 @@ namespace Xamarin.Forms.Platform.Android
class RendererHolder : Object
{
- public CellRenderer Renderer;
+ readonly WeakReference<CellRenderer> _rendererRef;
+
+ public RendererHolder(CellRenderer renderer)
+ {
+ _rendererRef = new WeakReference<CellRenderer>(renderer);
+ }
+
+ public CellRenderer Renderer
+ {
+ get
+ {
+ CellRenderer renderer;
+ return _rendererRef.TryGetTarget(out renderer) ? renderer : null;
+ }
+ set { _rendererRef.SetTarget(value); }
+ }
}
}
} \ No newline at end of file