Skip to content

Commit

Permalink
Fix for memory leak - #19
Browse files Browse the repository at this point in the history
  • Loading branch information
slodge committed Aug 1, 2012
1 parent 39bfe22 commit 8fc1af3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
53 changes: 47 additions & 6 deletions Cirrious/Cirrious.MvvmCross/Commands/MvxRelayCommand.cs
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
}
}
Expand Up @@ -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
Expand Down

0 comments on commit 8fc1af3

Please sign in to comment.