Permalink
Browse files

Fix for memory leak - #19

  • Loading branch information...
1 parent 39bfe22 commit 8fc1af3fc9960445b5358daf8111f6bc9683b907 @slodge slodge committed Aug 1, 2012
@@ -17,10 +17,11 @@ namespace Cirrious.MvvmCross.Commands
{
public class MvxRelayCommand
: MvxMainThreadDispatchingObject
- , IMvxCommand
+ , IMvxCommand
+ , IDisposable
{
- private readonly Func<bool> _canExecute;
- private readonly Action _execute;
+ private Func<bool> _canExecute;
+ private Action _execute;
public MvxRelayCommand(Action execute)
: this(execute, null)
@@ -73,12 +74,33 @@ public void RaiseCanExecuteChanged()
handler(this, EventArgs.Empty);
}
}
+
+ #region IDisposable implementation
+
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _execute = null;
+ _canExecute = null;
+ }
+ }
+
+ #endregion
}
- public class MvxRelayCommand<T> : IMvxCommand
+#warning Why is this not derived from MvxMainThreadDispatchingObject?
+ public class MvxRelayCommand<T>
+ : IMvxCommand
+ , IDisposable
{
- private readonly Func<T, bool> _canExecute;
- private readonly Action<T> _execute;
+ private Func<T, bool> _canExecute;
+ private Action<T> _execute;
public MvxRelayCommand(Action<T> execute)
: this(execute, null)
@@ -128,5 +150,24 @@ public void RaiseCanExecuteChanged()
handler(this, EventArgs.Empty);
}
}
+
+
+ #region IDisposable implementation
+
+ public void Dispose()
+ {
+ Dispose(true);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _execute = null;
+ _canExecute = null;
+ }
+ }
+
+ #endregion
}
}
@@ -1,8 +1,9 @@
using Cirrious.MvvmCross.Interfaces.Commands;
+using System;
namespace Cirrious.Conference.Core.ViewModels.Helpers
{
- public class WithCommand<T>
+ public class WithCommand<T> : IDisposable
{
public WithCommand(T item, IMvxCommand command)
{
@@ -11,6 +12,28 @@ public WithCommand(T item, IMvxCommand command)
}
public T Item { get; private set; }
- public IMvxCommand Command { get; private set; }
+ public IMvxCommand Command { get; private set; }
+
+ #region IDisposable implementation
+
+ public void Dispose ()
+ {
+ this.Dispose(true);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ var disposableCommand = Command as IDisposable;
+ if (disposableCommand != null)
+ {
+ disposableCommand.Dispose();
+ }
+ Command = null;
+ }
+ }
+
+ #endregion
}
}
@@ -21,6 +21,14 @@ public SessionGroup(TKey key, IEnumerable<SessionWithFavoriteFlag> items, Action
Key = key;
}
}
+
+ public override void OnViewsDetached ()
+ {
+ base.OnViewsDetached ();
+ Trace.Info("Views Detached - releasing list {0}", this.GetType().Name);
+ this.GroupedList.ForEach(x => x.ForEach(y => y.Dispose()));
+ this.GroupedList.Clear();
+ }
private List<SessionGroup> _groupedList;
public List<SessionGroup> GroupedList

0 comments on commit 8fc1af3

Please sign in to comment.