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 6155683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:28:39+00:00 2026-05-23T20:28:39+00:00

For some time now, I’ve observed an intermittent COM problem in my VSIX package

  • 0

For some time now, I’ve observed an intermittent COM problem in my VSIX package for Visual Studio 2010. Attempting to subscribe to one of the IDE’s COM-based event sinks randomly throws the following error:

COM object that has been separated from its underlying RCW cannot be used

A repro case boils down to this code (which must be used in VSIX, obviously):

using System;
using EnvDTE;
using EnvDTE80;

class Test
{
    private readonly Events _events;
    private readonly Events2 _events2;
    private readonly BuildEvents _buildEvents;
    private readonly ProjectItemsEvents _projectItemsEvents;

    public Test(IServiceProvider provider)
    {
        var dte = (DTE)provider.GetService(typeof(DTE));
        var dte2 = (DTE2)dte;

        // Store all references in fields as a GC precaution.
        _events = dte.Events;
        _events2 = (Events2)dte2.Events;
        _buildEvents = _events.BuildEvents;
        _projectItemsEvents = _events2.ProjectItemsEvents;

        // Proceed to subscribe to event sinks.
        _buildEvents.OnBuildBegin += BuildBeginHandler; // BOOM!
        _projectItemsEvents.ItemAdded += ItemAddedHandler;
    }

    private void ItemAddedHandler(ProjectItem projectItem) { }

    private void BuildBeginHandler(vsBuildScope scope, vsBuildAction action) { }
}

I’ve learned about a possible cause from numerous descriptions of similar problems that can be found on the net. It’s basically a side effect of the way Runtime Callable Wrappers and GC interact during COM interop. Here’s a similar problem complete with explanation.

I’m fine with that explanation, especially because it suggests an easy workaround – storing the event sink reference in a field in order to prevent it from being prematurely GC’ed. Indeed, many people seem to have solved their problem this way.

What bothers me is that it doesn’t work in my case. I’m really stumped as to why. As you can plainly see, I already store all object references in fields as a precaution. Yet the error still occurs. I tried being even more explicit using GC.KeepAlive() calls at the end of the ctor, but to no avail. Is there anything else left to do?

Without a solution, my VSIX randomly fails to load, leaving the user with a single option: to restart Visual Studio and hope it doesn’t happen the next time.

  • 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-23T20:28:40+00:00Added an answer on May 23, 2026 at 8:28 pm

    Well, I gave up and simply did the only thing that crossed my mind. I figured that since this is obviously a race condition I can’t affect in a predictable manner, I might as well reenter the race if I lose.

    So I moved the subscription lines into a while loop that try..catch-es them and retries after a bit of Thread.Sleep(). The loop exits either when both subscriptions succeed or when I’ve been continuously losing the race for more than 2 seconds.

    The kicker is, I haven’t lost the race once since I’ve implemented the change. A true Heisenbug, if I ever saw one.

    Anyway, I’m going to stick with this until a proper solution occurs to me or the bug reappears.

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

Sidebar

Related Questions

For some time now when I am debugging Visual C++ applications and viewing any
I have spent some time now to solve a problem for which i have
I have been looking for an answer to this problem for some time now
I've been using Zend framework some time now, but I'm facing a problem I
I've been using git for some time now on Windows (with msysGit) and I
I've been a web developer for some time now, and have recently started learning
I have looked at this page for some time now. Amazing, really. But I
I've been using NSViewAnimation for some time now and I've read somewhere that Core
I've been using JPA for some time now and been in projects where we've
I have been learning python for some time now. While starting this learning python

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.