CSLA .NET

From Rockford Lhotka's Expert C# 2008 and VB 2008 Business Objects books

Memory Leak with ReadOnlyBase derived object inside BusinessBase derived object

rated by 0 users
This post has 1 Reply | 2 Followers

Not Ranked
Posts 1
Cable Posted: Thu, Jun 18 2009 3:12 PM
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.
Top 10 Contributor
Posts 7,309
Thanks for following up on this - I'll add it to the list of things to look at.

Rocky

Page 1 of 1 (2 items) | RSS

Please contact Magenic for your .NET consulting
and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems