ActiveX COM not releasing all objects

I have a problem that may be too specific for me to get any help here, but I thought i would try. I've created an ActiveX connection with a COM object which is placed in my UI. The wrapper implements AxHost. The problem I am having is that when I close my View where the COM object resides, and then try to reopen it, I am not able to log in.

I am new to both ActiveX and COM, but I have figured out that I am not closing all objects properly. I've read up on and tried several solutions to this, but none of them seem to work. Currently, I have overridden the wrapper Dispose method to try to destroy the connection as follows:

protected override void Dispose(bool disposing)

As far as I can tell, ocx is the only COM object created. In the view class, I also try to get rid of all reference to the COM object like so:

protected void FormClosing(object sender, FormClosingEventArgs e)
                wrapper.CloseCanvasPage(canvasPageId, false);
                IsLoggedIn = !wrapper.Logout();
                wrapper = null;

                //  Garbage collect the unclosed ActiveX connections.
            catch (Exception ex) { }

I realize that manually calling garbage collection is bad, but several posts said this would get rid of stray connections. It doesn't seem to work.

In the parent, I call Dispose on the view, and set it to null, trying to break any connections, but I still have something hanging around. When I try to create a new connection, and call Initialized (a property included in the wrapper I am using), I get notification that the COM object is already initialized. However, if I try to log in, I get an error code stating that it is not initialized.

Does anyone have ideas on any obvious (or not so obvious) mistakes I may have made? I would be happy to provide more information if it would be helpful.


You're garbage-collecting the object correctly as far as I can see. The only thing I can suggest is to use C++/CLI for the wrapper. This way you wouldn't be using an RCW (Runtime Callable Wrapper) for the COM object, but the COM object directly. You would call IUnknown::Release directly. But I'm not sure it's a good idea to call IUnknown::Release multiple times until the reference count is 0 (like FinalReleaseComObject). If you have to do that, it means that some code is forgetting to Release the object (a memory leak), and using FinalReleaseComObject is a poor way to solve the problem.

It's also possible that there's just something wrong in your Initialized property, but I don't know anything about that.

In my experience (from a while ago though), you'll have to call GC.collect() TWICE, if you really want to get rid of ALL lingering objects.

Need Your Help

Angular js testing with Grunt

angularjs jasmine yeoman gruntjs

I am having problem with running test in my angularjs project -When I run "grunt test" I get

Custom UILabel not displaying properly in Interface Builder

ios swift interface-builder uilabel

I have a custom UILabel based class, that does not display properly in the storyboard.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.