views:

18

answers:

1

Hi there, we are having values stored in Cache-Enterprise library Caching Block. The accessors of the cached item, which is a List, modify the values. We didnt want the Cached Items to get affected.

Hence first we returned a new List(IEnumerator of the CachedItem) This made sure accessors adding and removing items had little effect on the original Cached item.

But we found, all the instances of the List we returned to accessors were ALIVE! Object relational Graph showed a relationship between this list and the EnterpriseLibrary.CacheItem.

So we changed the return to be a newly cloned List. For this we used a LINQ say (from item in Data select new DataClass(item) ).ToList() even when you do as above, the ORG shows there is a relationship between this list and the CacheItem.

Cant we do anything to create a CLONE of the List item which is present in the Enterprise library cache, which DOESNT have ANY relationship with CACHE?!

A: 

You would have to make a deep clone of the list, i.e., add a clone of each object in the list (and clone any objects that they may contain, I would suggest having a Clone() method to do this for you), to a new list. As was commented above, a reference is a reference no matter how many times you copy it.

David
Yeah cloning is my intention. the Class has properties only of Value Types. So a basic LINQ should do the magic right?! (from item in Data select new DataClass(item) ).ToList()
ioWint
Infact i profiled again WITHOUT Cloning... Everytime u retrieve the cache item if it exists and send it to the caller.. we are getting a new list which is stuck in memory! with a object relationship graph showing its attached to CacheItem!
ioWint
the list itself as you are creating it without cloning shouldn't get stuck in memory, unless you are using static? The new list without cloning should basically be a list of pointers to the cached elements.
David
yes the Method which retrieves data is a Static method. But i dont understand why it should create an issue.
ioWint
you have to be careful with statics, they are notorious for causing objects to never be garbage collected.
David