Suppose I have a list of COM objects as a property in a class :
public List<LegacyCOMObjects> LegacyCOMObjects { get; set; }
What is the best way to set this property with a new value? Should I do it the classic .NET way or should I release the objects before setting it because memory leaks may happen? I was think of something like this :
private List<LegacyCOMObjects> _legacyCOMObjects ;
public List<LegacyCOMObjects> LegacyCOMObjects
{
get { return _legacyCOMObjects; }
set
{
if (_legacyCOMObjects!= null)
{
_legacyCOMObjects.ForEach(o =>
{
Marshal.ReleaseComObject(o);
o = null;
});
}
_legacyCOMObjects= value;
}
}