Share via

checking dte.Solution for null causes a System.AccessViolationException?

Rudolf Meier 291 Reputation points
2022-01-14T19:18:22.85+00:00

Hi

In my C# Visual Studio Extension (for 2017 and 2019 at the moment), I'm using this lines of code...

try
{
    if(dte.Solution != null)
        ... do something;
}
catch(Exception)
{ /*ok, in this case, I don't care*/ }

And... the problem is, that Visual Studio (2017 and 2019) crashes in this if-statement with an unhandled System.AccessViolationException ... why? (the debugger shows, that dte is a valid object, but Solution ... might not be... no idea if it is or not... even the debugger can't tell me... that's why I thought... try-catch is the solution?)

Rudolf

Community Center | Not monitored
0 comments No comments

1 answer

Sort by: Most helpful
  1. Michael Taylor 61,221 Reputation points
    2022-01-14T20:11:11.907+00:00

    DTE is a COM object. If you try to reference that COM object on a thread other than the one it was created on then bad things happen. In your specific case it is unclear where this code is being called from but if it isn't on the UI thread of VS then you'll get odd behavior like this. This is a total guess at this point given no other context. It is also dependent upon what type of extension you're building - package, addin, etc.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.