Thanks David,
yep, that's essentially my problem, but unfortunately I have no idea how
many events will be hooked up.
Its a status indicator that keeps track of the current loading status. ie
Loading, Searching, Normal, etc. (each one being an enum). I wanted to put
something into the baseviewmodel in the dispose (as that's where the
property is and is being bound to.)
OH. I've just realised something as I typed that. The Views are binding to
the state of the page via a property on the viewmodel (as you do). I wonder
if this Status class would be better off as an instance rather than a
Static class.
Ok, now thats even more weird. I just checked the viewmodel of one of these
and its creating a new instance of the class, which means there's already
an instance per page. The operators + and - are, however, static.
So my calling Clear on the instance (which I'm doing) is clearing the
internal list of the instance. The internal list is not static. It would
seem that I may already have a working way of clearing my statuses without
a leak. I just assumed because they were static that another viewmodel
calling clear would clear all statuses.
private readonly List<Status> _statusList = new List<Status>();
public static StatusHandler operator +(StatusHandler handler,
Status status)
{
handler._statusList.Add(status);
handler.RaisePropertyChanged("Value");
handler.RaisePropertyChanged("Count");
return handler; }
public void Clear()
{
_statusList.Clear();
this.RaisePropertyChanged("Value");
this.RaisePropertyChanged("Count");
}
I wonder if those operator method calls should be static or not.
I'll have a look at the replied that have come in while I was writing this.
Michael, yours uses a static list.
Sorry if my post has been a bother. I've certainly learned something from
the replies even if my code already works (I think?)
Oh, to be clear, the leak was there but already existing code. I found the
leak and added the clear to fix the leak, but then thought I'd be clearing
all registrations from all viewmodels rather than just the one I wanted (as
it was pointed out the leak was occuring because its a static call).
thanks all!
Stephen
On Mon, Apr 2, 2012 at 9:27 AM, David Walker <[email protected]> wrote:
> http://msdn.microsoft.com/en-us/library/ee658248.aspx search on the page
> for 'static event' should find the best ways to do this.
>
> Typically the child will unsubscribe itself inside Dispose()
>
> On 2 Apr 2012, at 02:21, Stephen Price wrote:
>
> > Hey all,
> >
> > I've got a Static class that's behaving like an EventHandler in that you
> assign values to it via an operator + or - method.
> >
> > I need a method on this thing or some way that I can unhook the ones
> from one particular class (called from the dispose of that class) so that
> it can remove only the ones that were added from that class. Its accepting
> an Enum, so is there a way to see the owner of an Enum or where it was
> created?
> >
> > Or do I need to store something along with the Enum that can later be
> used to identify who attached it?
> >
> > If you imagine it as a Static eventhandler, how can I look through the
> references and only unhook the ones from a particular class. (assuming
> internally you can loop through each handler.
> > The actual problem is that it's causing a memory leak if I don't unhook
> these things when that page/veiwmodel is disposed.
> >
> > hope the above makes some kind of sense to someone. :)
> >
> > cheers,
> > Stephen
>
>