views:

21

answers:

1

The vb.net "WithEvents" syntax is very useful, but if a WithEvents field which references a long-lived object is not nulled out, it will often constitute a memory leak.

Is there any easy way for a Dispose routine to have vb.net automatically clear out all WithEvents fields and unsubscribe them?

I've figured out a nice way to wrap the creation of disposable fields so that they will be automatically Disposed when the containing object is disposed, without having to individually list such objects. Is there any way to take care of WithEvents fields without having to manually null them out in the Dispose routine?

EDIT Since there was no response indicating a built-in way to do it without reflection, is there any built-in means of using reflection to determine whether a particular property is a VB automatic implementation of a WithEvents "field"? I know that a WithEvents "field" called "foo" is implemented as a property called "foo" and a field called "_foo", but that's such a common naming pattern that I wouldn't count on it. Is there some attribute that could be used to determine which properties need to be auto-nulled?

+1  A: 

This may be possible with a bit of reflection magic. But there is no language intrinsic way of querying Fields which are tagged with WithEvents

JaredPar
@JaredPar: Bummer. Is there any way to have a method both engage an event subscription and keep a reference to use when unsubscribing? For iDisposables, I have a generic RAII function which adds a disposable to a list and returns it. When an object is disposed, all iDisposalbles it has thus registered will be Disposed. Any possibility of doing likewise with event subscriptions?
supercat