Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » Code Snippets » General

How to free the COM object which holds references to resources in a timely manner?


Posted Date:     Category: General    
Author: Member Level: Gold    Points: 15


In managed code, if application is executing RCW and we release RCW at that time then it will encounter errors so releasing objects activities should be done with proper care. This code is simple to use and understand. I hope it will be useful for you.



 


How to free the COM object which holds references to resources in a timely manner?
Purpose:
Object is passed as a parameter in the method so that it will be released whenever it is no longer in use.
The ReleaseComObject method decrements the reference count of an RCW.
GC.Collect method is used to force garbage collection.
If there is any error then messagebox is used to show that error.
Version support:
This method can be used for .Net framework versions 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0


private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "How to free the COM object which holds references to resources in a timely manner?"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to add items of array to combobox using c#?
    Previous Resource: How to insert new record in database using c#?
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.