Barry said:
My class raises an event after a transaction has been completed from within
it. The event is a notification to the UI that the transaction was successful
and contains, in its args, contextual information.
Let me try to give a better illustration of my problem.
I have a custom event in Class A. It is raised whenever a specific method
call to it has been successful.
Class B is a facade class that instantiates ClassA objects, listens for
ClassA events, and re-raises a new event that is consumed by the UI.
The order of events looks like this:
1-The UI creates a new instance of ClassB.
2-ClassB creates an instance of ClassA.
3-ClassA receives a method call that says "Save data" and, if successful,
raises a DataSaved event.
4- ClassB handles the ClassA event and raises a wrapper event that passes
the event args up to the UI.
5- In ClassB, the reference to ClassA is set to nothing.
6- In the UI, the reference to ClassB is set to Nothing.
This is bad form. Setting references to Nothing (null) generally
accomplishes nothing -- it does make objects eligible for garbage collection
if there are no other references, but unless the referenced object is the
parent of a huge collection of other objects you no longer need, this is
unlikely to be of concern.
Keep in mind that the garbage collector isn't guaranteed to run in any
amount of time, and setting a reference to null does not force deallocation
of any object. The only thing that nulling out a reference achieves is that
code can no longer *accidentally* use the object. It doesn't make any
*functional* difference.
If you're replacing the reference with a new instance, you can just do that,
and you don't have to bother with setting it to null first.
The second time through, the event handler in ClassB receives two events
from ClassA. The code in ClassA that raises the event is not hit twice,
however.
Here's where your description gets into trouble. Do you mean "the event
handler in a particular instance of class B receives two events from a
particular instance of class A"? Have you checked whether "this" (Me) is the
same in both cases?
If B attached an event handler to the same event twice, this would be the
behavior to expect: the event handling code only runs once, but it calls
both attached delegates.
This is what leads me to believe that the previous instance of
ClassA is still in memory. However, I would assume that, even though the
object still lives in memory, all references to it, including delegates,
should not be accessible.
You're formulating it backwards. An object still lives in memory if there
are still references to it. If there are no references to it, it's no longer
"alive" (its bits might still be around, but you have no way to tell, by
definition). So either there are no references and there is no object, or
there are still references and there is an object. It cannot be that there
is an object when there are no references -- that just means you've got the
wrong idea about the references.
Your code doubtlessly has a bug, but exactly what it is I still can't say.
![Smile :-) :-)](/styles/default/custom/smilies/smile.gif)