This is a follow up to a discussion I had with Rocky at VSLive! Las Vegas last week.
I have BusinessBase derived objects that have ReadOnlyBase derived objects in them. When importing data from an access db and saving it to the new SQL DB, I received an OutOfMemoryException.
The leak disappears if I change the BusinessBase OnAddEventHooks to not subscribe to the BusyChanged, UnhandledAsyncException, and PropertyChanged events for IReadOnly objects or if I don't use managed backing fields and Load/Set/Get/Read Property methods in my business objects. Here's what I did to the OnAddEventHooks method in BusinessBase:
protected virtual void OnAddEventHooks(IBusinessObject child)
{
bool isReadOnly = child is IReadOnlyObject;
INotifyBusy busy = child as INotifyBusy;
if (busy != null && !isReadOnly)
busy.BusyChanged += new BusyChangedEventHandler(Child_BusyChanged);
INotifyUnhandledAsyncException unhandled = child as INotifyUnhandledAsyncException;
if (unhandled != null && !isReadOnly)
unhandled.UnhandledAsyncException += new EventHandler(Child_UnhandledAsyncException);
INotifyPropertyChanged pc = child as INotifyPropertyChanged;
if (pc != null && !isReadOnly)
pc.PropertyChanged += new PropertyChangedEventHandler(Child_PropertyChanged);
IBindingList bl = child as IBindingList;
if (bl != null)
bl.ListChanged += new ListChangedEventHandler(Child_ListChanged);
INotifyChildChanged cc = child as INotifyChildChanged;
if (cc != null)
cc.ChildChanged += new EventHandler(Child_Changed);
}
I've posted more detail and screen shots from Ants on my blog http://lovetocode.net
Let me know if you need any more details.
I have BusinessBase derived objects that have ReadOnlyBase derived objects in them. When importing data from an access db and saving it to the new SQL DB, I received an OutOfMemoryException.
The leak disappears if I change the BusinessBase OnAddEventHooks to not subscribe to the BusyChanged, UnhandledAsyncException, and PropertyChanged events for IReadOnly objects or if I don't use managed backing fields and Load/Set/Get/Read Property methods in my business objects. Here's what I did to the OnAddEventHooks method in BusinessBase:
protected virtual void OnAddEventHooks(IBusinessObject child)
{
bool isReadOnly = child is IReadOnlyObject;
INotifyBusy busy = child as INotifyBusy;
if (busy != null && !isReadOnly)
busy.BusyChanged += new BusyChangedEventHandler(Child_BusyChanged);
INotifyUnhandledAsyncException unhandled = child as INotifyUnhandledAsyncException;
if (unhandled != null && !isReadOnly)
unhandled.UnhandledAsyncException += new EventHandler(Child_UnhandledAsyncException);
INotifyPropertyChanged pc = child as INotifyPropertyChanged;
if (pc != null && !isReadOnly)
pc.PropertyChanged += new PropertyChangedEventHandler(Child_PropertyChanged);
IBindingList bl = child as IBindingList;
if (bl != null)
bl.ListChanged += new ListChangedEventHandler(Child_ListChanged);
INotifyChildChanged cc = child as INotifyChildChanged;
if (cc != null)
cc.ChildChanged += new EventHandler(Child_Changed);
}
I've posted more detail and screen shots from Ants on my blog http://lovetocode.net
Let me know if you need any more details.