We help IT Professionals succeed at work.

Issu on deserialize an object, memory leak?

CodingCat
CodingCat asked
on
hi experts,

I would like to implement a way for user to load configurations from an .xml file and store it to an object (say a List<object> called OB).  The users may allow to open different .xml files during the process. Each time they open a different xml file, the data store OB must also be updated.
The way I'm doing it now is write a static function which will return a new List<object> from the xml file.

// class OpenSave
public static List<Object> Deserialize(string file)
{
    List<Object> myObject= new List<Object>();
..........// Deserialize...
    Return myObject;
}

When user load a different file, the following code is called:
OB.clear();
OB = OpenSave.Deserialize(filename);

My question is, what about the old object pointed to by OB?  It seems to me the static function Deserialize create a new List object each time it is called and make OB point to this new object. Is this going to cause something like memory leak?
Comment
Watch Question

Hi,

Once the list, or other object, no longer has a reference to it, it will become eligible for Garbage Collection.

See this for a quick explanation

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7c744680-2cd3-4538-a5a1-001e64989472

This is a bit lengthier
http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

There should also be more stuff on your install of MSDN (if you have it)

Good luck