Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. IDisposable with COM

IDisposable with COM

Scheduled Pinned Locked Moved C#
comregexquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I have an application that uses a COM object to make a connection to an application. I need to make sure that when my application ends that I call the EndSession and CloseConnection methods of this COM object. I was reading this article: Implementing IDisposable and the Dispose Pattern Properly[^] as I think this is what I would need to do to make sure my closing code always gets called, but it doesn't seem to happen when I stop debugging. The application that I am connecting to still thinks my program is connected. It seems like when I stop debugging (or if I have a program crash, that the Dispose methods don't get called. I also tried to add a Finalizer but that causes a System.Runtime.InteropServices.InvalidComObjectException stating that the COM object that has been separated from it's underlying RCW cannot be used.

    private bool disposed;
    protected virtual void Dispose(bool disposing)
    {
    if (!disposed)
    {
    if (_Session != null) // _Session is the COM object.
    {
    _Session.EndSession(); // These are the COM functions to close and end the
    _Session.CloseConnection(); // session that must be called, but this is where I
    _Session = null; // get the InvalidComObjectException.
    }
    disposed = true;
    }
    }
    public void Dispose()
    {
    Dispose(true);
    GC.SuppressFinalize(this);

    }
    ~SessionConnection() // This was added, which seems to be the root cause of the COM exception
    {
    Dispose(false);
    }

    I'm not sure what is the best way to ensure that the EndSession and CloseConnection methods of my COM object get called even if my program crashes or I stop debugging.

    L D 2 Replies Last reply
    0
    • H hpjchobbes

      I have an application that uses a COM object to make a connection to an application. I need to make sure that when my application ends that I call the EndSession and CloseConnection methods of this COM object. I was reading this article: Implementing IDisposable and the Dispose Pattern Properly[^] as I think this is what I would need to do to make sure my closing code always gets called, but it doesn't seem to happen when I stop debugging. The application that I am connecting to still thinks my program is connected. It seems like when I stop debugging (or if I have a program crash, that the Dispose methods don't get called. I also tried to add a Finalizer but that causes a System.Runtime.InteropServices.InvalidComObjectException stating that the COM object that has been separated from it's underlying RCW cannot be used.

      private bool disposed;
      protected virtual void Dispose(bool disposing)
      {
      if (!disposed)
      {
      if (_Session != null) // _Session is the COM object.
      {
      _Session.EndSession(); // These are the COM functions to close and end the
      _Session.CloseConnection(); // session that must be called, but this is where I
      _Session = null; // get the InvalidComObjectException.
      }
      disposed = true;
      }
      }
      public void Dispose()
      {
      Dispose(true);
      GC.SuppressFinalize(this);

      }
      ~SessionConnection() // This was added, which seems to be the root cause of the COM exception
      {
      Dispose(false);
      }

      I'm not sure what is the best way to ensure that the EndSession and CloseConnection methods of my COM object get called even if my program crashes or I stop debugging.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I doesn't "seem" like you are sure if Dispose() is getting called so you should add a "log entry" in Dispose() to be sure. Your "session" instance looks private. You could try (pardon the pun), a higher level "try ... finally", where the finally always runs and checks your object for null, disposes it, etc. Then there are application level exception handlers and "exits" ... you need to log to understand which paths your particular app can end on.

      1 Reply Last reply
      0
      • H hpjchobbes

        I have an application that uses a COM object to make a connection to an application. I need to make sure that when my application ends that I call the EndSession and CloseConnection methods of this COM object. I was reading this article: Implementing IDisposable and the Dispose Pattern Properly[^] as I think this is what I would need to do to make sure my closing code always gets called, but it doesn't seem to happen when I stop debugging. The application that I am connecting to still thinks my program is connected. It seems like when I stop debugging (or if I have a program crash, that the Dispose methods don't get called. I also tried to add a Finalizer but that causes a System.Runtime.InteropServices.InvalidComObjectException stating that the COM object that has been separated from it's underlying RCW cannot be used.

        private bool disposed;
        protected virtual void Dispose(bool disposing)
        {
        if (!disposed)
        {
        if (_Session != null) // _Session is the COM object.
        {
        _Session.EndSession(); // These are the COM functions to close and end the
        _Session.CloseConnection(); // session that must be called, but this is where I
        _Session = null; // get the InvalidComObjectException.
        }
        disposed = true;
        }
        }
        public void Dispose()
        {
        Dispose(true);
        GC.SuppressFinalize(this);

        }
        ~SessionConnection() // This was added, which seems to be the root cause of the COM exception
        {
        Dispose(false);
        }

        I'm not sure what is the best way to ensure that the EndSession and CloseConnection methods of my COM object get called even if my program crashes or I stop debugging.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        When you launch the code from inside Visual Studio, it's launched, the debugger is attached and you debug on your merry way. If you hit Stop inside Visual Studio, your code is stopped, wherever it is, and removed from memory. Your Dispose code never gets executed. The only way to make sure it does is if you exit your application as a user would, gracefully.

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        OriginalGriffO H 2 Replies Last reply
        0
        • D Dave Kreskowiak

          When you launch the code from inside Visual Studio, it's launched, the debugger is attached and you debug on your merry way. If you hit Stop inside Visual Studio, your code is stopped, wherever it is, and removed from memory. Your Dispose code never gets executed. The only way to make sure it does is if you exit your application as a user would, gracefully.

          A guide to posting questions on CodeProject

          Click this: Asking questions is a skill. Seriously, do it.
          Dave Kreskowiak

          OriginalGriffO Online
          OriginalGriffO Online
          OriginalGriff
          wrote on last edited by
          #4

          :thumbsup: That's what I was going to say! The only thing I'd add is that the same thing will happen if you use Task Manager to kill the process. So if this is causing a problem with "Stop debugging" it'll also cause the same problem if the user terminates the app.

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • D Dave Kreskowiak

            When you launch the code from inside Visual Studio, it's launched, the debugger is attached and you debug on your merry way. If you hit Stop inside Visual Studio, your code is stopped, wherever it is, and removed from memory. Your Dispose code never gets executed. The only way to make sure it does is if you exit your application as a user would, gracefully.

            A guide to posting questions on CodeProject

            Click this: Asking questions is a skill. Seriously, do it.
            Dave Kreskowiak

            H Offline
            H Offline
            hpjchobbes
            wrote on last edited by
            #5

            I never knew that about the debugger, thanks! But then does this mean that GC doesn't do any cleanup? Or is Visual Studio maintaining the information so it knows to release any allocated memory? Is this something to do with the VS Hosting Process? If I open a file within a using statement, it seems like stopping the debugger is still releasing the file handle (at least I think so, I'm still very new). Are there any recommendations / best practices when dealing with situations like this?

            D 1 Reply Last reply
            0
            • H hpjchobbes

              I never knew that about the debugger, thanks! But then does this mean that GC doesn't do any cleanup? Or is Visual Studio maintaining the information so it knows to release any allocated memory? Is this something to do with the VS Hosting Process? If I open a file within a using statement, it seems like stopping the debugger is still releasing the file handle (at least I think so, I'm still very new). Are there any recommendations / best practices when dealing with situations like this?

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              There is no cleanup of the orphaned COM stuff. It's just left hanging. When the code is stopped abruptly, the GC doesn't even run. Your process is stopped cold and the memory the app and its data was occupying freed and returned to Windows. The GC cannot clean up non-managed resources anyway. It can only cleanup managed stuff. The closest it can get is to free up the managed COM-wrapper that's is hanging onto the unmanaged COM references, but this doesn't free up those unmanaged resources. They are just orphaned. The debugger doesn't release anything at all. It's just attached to the process, not executing or controlling it in any meaningful way.

              A guide to posting questions on CodeProject

              Click this: Asking questions is a skill. Seriously, do it.
              Dave Kreskowiak

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups