Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 559343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:13:42+00:00 2026-05-13T12:13:42+00:00

I am writing an excel class and i want to release this unmanaged object

  • 0

I am writing an excel class and i want to release this unmanaged object automatically.
I’m using IDisposable pattern and write Dispose methods.
Example ;

class MSExcel : IDisposable
{
    ApplicationClass excel;
    bool disposed;

    public MSExcel()
    {
         disposed = false;
         excel = new ApplicationClass();
    }

    public void Dispose(bool disposing)
    {
        if (!this.disposed)
        {

            if (disposing)
            {

            }

            excel.Quit();                
            disposed = true;

        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    ~MSExcel()
    {
       Dispose(false);
    }
}

But i have classical error on exc.Quit().
“COM object that has been separated from its underlying RCW”.
Have any my mistake in code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:13:42+00:00Added an answer on May 13, 2026 at 12:13 pm

    As explained in my answer to your other related question here, you should not be taking actions on reference types from within your finalizer. You enforce this by making use of the bool disposing parameter of your Disposed(bool) method, as you have done. You pass in true, when Disposed(bool) is called explicitly from the void Dispose() method, and pass in false when called from your finalizer, which you have also done.

    However, you also need to protect your call to excel.Quit() so that it is not called when Disposed(bool) is called via the finalizer. That is, you should only call excel.Quit() when the bool disposing argument is true.

    Therefore, the code for your Disposed(bool) method should look as follows:

    public void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
    
            if (disposing)
            {
                excel.Quit();   
            }
    
            disposed = true;
        }
    }
    

    Once done, you could make use of your “MSExcel” class as follows:

    using (MSExcel msExcel = new MSExcel)
    {
       // The code calling your 'MSExcel' object goes here.
    }
    

    By doing it this way, when your code gets to the closing bracket, “}”, of your using statement block, the Dispose method on your ‘MSExcel’ class will be called automatically, ensuring that excel.Quit() is called deterministically, and not from a finalizer.

    Hope this helps…

    Mike

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an Excel File using Apache POI. I want to write in
Why can I not write values to Excel using the Worksheet class, or Sheet
I work on writing the excel Add-In using Excel Interop. I want to get
I'm writing a Excel class using Microsoft.Interropt.Excel DLL. I finish all function but I
I have an Python ExcelDocument class that provides basic convenience methods for reading/writing/formatting Excel
I'm writing an Excel macro , and I'm having trouble clearing a Scripting.Dictionary object
I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference.
I am writing an application that extracts images from an Excel spreadsheet using COM
I started writing a small wrapper class to take care of my excel operations:
I am writing an excel report using an action,controller, servlet struts framework. The report

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.