Best practice for EntityCollection cleanup

Posts   
 
    
rracer99
User
Posts: 58
Joined: 11-Mar-2007
# Posted on: 05-Jul-2007 08:14:19   

Should all EntityCollections have Dispose() or Clear() (which one, both?) called after use or is it safe to assume the GC will take care of this after ASP.NET requests to the web server are terminated?

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 05-Jul-2007 09:14:37   

Dispose is only needed when an object holds some unmanaged resources/code. So it's fine to leave it to the GC.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39912
Joined: 17-Aug-2003
# Posted on: 05-Jul-2007 09:54:58   

There's a caveat with respect to clear. If you have an entity and it's in collections C1 and C2, and C2 goes out of scope (so should be cleaned up), it won't as the event handler in C2 keeps it in memory as it's bound to the changed event of the entity which is still alive.

Normally this won't happen much (if at all) but in case you run into this cornercase, be aware of this to clear C2 if the entity inside C2 is kept alive but the collection isn't.

Frans Bouma | Lead developer LLBLGen Pro