I believe that I've found a bug in the BusinessLayer code involving the ParentBusinessObjectChanged event. A reference to a null pointer is causing the application to throw an exception.
I have a ParentBO and ChildBO, with the proper ParentRelationship set up. The ChildBO "attaches" to the ParentBO's FieldPropertyChanged event in order to listen for changes to field values.
The ChildBO has a handler for its ParentBusinessObjectChanged event as follows:
private void ChildBO_ParentBusinessObjectChanged(ParentBusinessObjectChangedEventArgs e)
{
ParentBO parentBO;
parentBO = e.PreviousParentBusinessObject as ParentBO;
if (parentBO != null) {
// Detach from old Parent BO
parentBO.FieldPropertyChanged -= _parentBO_FieldPropertyChanged;
}
parentBO = e.NewParentBusinessObject as ParentBO;
if (parentBO != null) {
// Attach to the new Parent BO
parentBO.FieldPropertyChanged += _parentBO_FieldPropertyChanged;
}
}
The problem occurs when the ParentBO is disposed. The best I can tell, the sequence of events leading to the null reference exception is a follows:
- The application's form is closed, which causes...
- The ParentBO to be disposed, which eventually causes...
- The BusinessLayer.Dispose procedure to be called, which calls...
- The BusinessLayer.ForciblyRemoveAllHandlers() routine, which...
- Sets the value BusinessLayer._Handlers_MiscEvents to null, then...
- The BusinessLayer.Dispose procedure eventually calls the CleanUp() routine, which...
- Sets the ChildBO's ParentBusinessObject to null, which...
- Fires the ChildBO's ParentBusinessObjectChanged event with the e.PreviousParentBusinessObject==ParentBO, which...
- Tries to detach from the ParentBO's FieldPropertyChanged event, which...
- Causes a null reference exception in BusinessLayer.RemoveEventHandler route, because...
- The ParentBO's BusinessLayer._Handler_MiscEvents was set to null in step #5
Hope this helps toward an eventual fix.