I've a C# class which uses a COM component using interop.
Here is the sample code:
public class MyClass
{
MyCOMClass myObj=null;
try
{
myObj = new MyCOMClass();
for (int num = 0; num < myArr.Length; num++)
{
//Call a method on myObj and do some processing
}
}
catch(Exception ex)
{
//log exception
}
finally
{
if (myObj != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(myObj);
}
}
}
Here, the for loop runs for about 150 times.
n executing this code, in the catch block, am getting an exception :
"COM object that has been separated from its underlying RCW cannot be used."
I tried implementing IDisposable interface and then writing Dispose method:
public class MyClass: IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
protected virtual void Dispose(bool diposeMgdResources)
{
if (!this.disposed)
{
if (diposeMgdResources)
{
if (myObj != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(myObj);
}
}
this.disposed = true;
}
}
}
From the client, I then call dispose on this class like:
myClass.Dispose();
But, still am getting the same exception here.What am I missing?
Thanks for reading.
Take a look at this article:
Marshal.ReleaseComObject considered dangerous
You probably don't want to call Marshal.ReleaseComObject().
Related
I'm new to IDisposable concept, I have the following scenario
facing an exception(stackover flow) when I dispose of the object.
I have to create multiple requests(parallel) to get a response from
API.
Like below:
ViewModel
public vm
{
vm(IunityContainer container) { }
public void somemethod
{
await ListofItems.ParallelForEachAsync(async item =>
{
try
{
using (var objectService = _container.Resolve<IobjectService>())
{
var response = await objectService.GeAllItem(request);
concurrentBag.Add(response);
}
}
catch (Exception e)
{
_logger.Error("Failed operation.", e);
}
},maxDegreeOfParallelism: 5);
}
}
Service.cs
public class Service : Idisposable
{
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
this.Dispose();// not sure how to dispose the service layer object which is created
}
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Issue: since I'm creating multiple objects by a parallel thread when end of the using (}) it throws stackoverflow exception.
solution: I wanted to dispose of "objectService" which is creating
each iteration of the foreach.
please help me resolve this issue.
The stackoverflow error comes from the fact that Dispose() calls Dispose(bool disposing) which back again calls Dispose() on so on until you reach the end of the stack.
It is not a problem of multithreading.
If you look again at the example for IDisposable object given by Microsoft https://learn.microsoft.com/fr-fr/dotnet/api/system.idisposable?view=netframework-4.8 you will see that inside if (disposing) you need to call Dispose on all IDisposable members that the class contains and not on the class itself (not this.Dispose())
I have a class with resources which I need to dispose:
class Desert: IDisposable
{
private object resource; // handle to a resource
public Desert(string n)
{
// Create resource here
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (resource != null) resource.Dispose();
}
}
}
I wonder if there is any way to automatically ask framework to call Dispose on each element, whenever the List object is going to be destroyed, just like I would have destructor. Currently I am looping through the list and calling Dispose:
// On form open
List<Desert> desertList = new List<Desert>();
for(int i = 0; i < 10; i++)
{
desertList.Add(new Desert("Desert" + i));
}
// On form closing
for (int i = 0; i < 10; i++)
{
desertList[i].Dispose();
}
Is it the only way to dispose objects inside List?
You could extend the List type itself:
public class AutoDisposeList<T> : List<T>, IDisposable where T : IDisposable
{
public void Dispose()
{
foreach (var obj in this)
{
obj.Dispose();
}
}
}
using (var myList = new AutoDisposeList<Desert>())
{
}
If you need more than that, you could look at Finalizers. These aren't quite destructors - they are methods that run JUST before an object is Garbage Collected. It's very easy to go wrong with these though, my vote is for a using statement.
see: https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.100).aspx
I application that uses a a Interop Com Object. Therefore I had written a wrapper class, to do the freeing in the dispose or if this is not done in the finalizer. So I can use the using keyword, to ensure a freeing is done.
Is using this pattern a good way? Or is there even a class in the Framework that is doing this for me?
class ComWrapper<T> : IDisposable
{
private readonly T comObject;
private bool disposed = false;
public ComWrapper(T comObject)
{
this.comObject = comObject;
}
~ComWrapper()
{
this.Dispose(false);
}
public T ComObject
{
get
{
return this.comObject;
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
if (this.disposed)
{
return;
}
Marshal.FinalReleaseComObject(this.comObject);
}
}
I personally wouldn't recommend this because it seems somewhat pointless. But, I would recommend having a static class called Utility, with this method:
public static void disposeComObject<T>(ref T comObject)
{
if(Marshal.IsComObject(comObject) && comObject != null)
{
//You need to save the object
if(typeof(T) == typeof(Microsoft.Office.Interop.Excel.Workbook))
{
((Microsoft.Office.Interop.Excel.Workbook))comObject.Save();
((Microsoft.Office.Interop.Excel.Workbook))comObject.Close();
}
//You need to save the object
if(typeof(T) == typeof(Microsoft.Office.Interop.Excel.Application))
{
((Microsoft.Office.Interop.Excel.Application))comObject.Quit();
}
Marshal.ReleaseComObject(comObject);
comObject = null;
}
}
Now From Code, you can call this like so
...
Microsoft.Office.Interop.Excel comObject = null;
try{
//Open comObject
//Here I would call some functions, and have nested exceptions
}
catch(nestedException err)
{
//Handle at your discretion
}
finally{
Utility.disposeComObject(ref comObject);
}
This is specific to the Excel Namespace, but adjusting it should be easy enough.
Create class MustDispose.cs
public class MustDispose
{
public MustDispose()
{
}
}
in.aspx Page
protected void Page_Load(object sender, EventArgs e)
{
using (MustDispose obj = new MustDispose)
{
// use the object
}
}
errorr
Destructors and object.Finalize cannot be called directly. Consider
calling IDisposable.Dispose if available.
You class has to impplement IDisposable, in oder to be used inside using statement.
Example:
public class MustDispose : IDisposable
{
public MustDispose()
{
}
//implement Dispose
public void Dispose()
{
....
}
}
reading from using:
Provides a convenient syntax that ensures the correct use of
IDisposable objects.
You need to implement IDisposable like so:
class TestDispose : IDisposable
{
public TestDispose()
{
...
}
public void Dispose()
{
// dispose of your resources here
}
}
Then you will be able to use it in a using block which automatically calls obj.Dispose at the end of the block.
using (var obj = new TestDispose())
{
...
}
Is like:
var obj = new TestDispose();
try
{
...
}
finally
{
obj.Dispose();
}
I have a COM type (created using tlbimp.exe) and a C# class that wraps this object. I want to perform some clean up in the finalizer for my C# wrapper. Following the guidelines here I might write something like this:
public class MyClass : IDisposable
{
private IMyComObject comObject;
public MyClass()
{
comObject = new MyComObject();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~MyClass()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
// Be tollerant of partially constructed instances
if (comObject != null)
{
comObject.Cleanup();
// Account for object being disposed twice
comObject = null;
}
}
// Other bits go here...
}
I know that finalizers can run in any order and so I should not attempt to use any object that implements a finalizer, however as far as I can tell tlbimp generated COM types don't implement a finalizer and so the above should be OK.
I haven't been able to find any official documentation on this however, so my question is is it safe to reference and use COM objects in a finalizer?
I created a abstract com wrapper class, from which I derive all my com wrapper classes. It works very well. My original code is VB, since I need late binding (this was before C# introduced the dynamic type). The rest of my app is written in c#.
public abstract class ComWrapper : IDisposable
{
protected internal object _comObject;
protected ComWrapper(object comObject)
{
_comObject = comObject;
}
#region " IDisposable Support "
private bool _disposed = false;
protected virtual void FreeManagedObjects()
{
}
protected virtual void FreeUnmanagedObjects()
{
ReleaseComObject(_comObject);
}
private void Dispose(bool disposing)
{
if (!_disposed) {
if (disposing) {
FreeManagedObjects();
}
FreeUnmanagedObjects();
_disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected override void Finalize()
{
Dispose(false);
base.Finalize();
}
#endregion
}
and
public static class Helpers
{
public static void ReleaseComObject(ref object o)
{
if ((o != null)) {
try {
Marshal.ReleaseComObject(o);
} catch {
} finally {
o = null;
}
}
}
public static string ToDotNetString(object comString)
{
if (comString == null) {
return string.Empty;
}
string s = comString.ToString();
ReleaseComObject(ref comString);
return s;
}
}