Debugging AutoCAD using Visual Studio 2013

There are lots of reasons to want to use the newly-released Visual Studio 2013 to develop and debug .NET modules for AutoCAD. One of the main ones is the long-awaited addition of Edit & Continue support for 64-bit applications. Unfortunately in this post we'll see why, despite the wait, E&C isn't going to work when debugging 64-bit .NET modules inside AutoCAD.

The primary issue when running AutoCAD from the VS2013 debugger manifests itself in issues with font loading. If you launch AutoCAD from VS2013 to debug a 64-bit class library – even without actually loading the module – then AutoCAD will crash when you run commands such as STYLE (which attempts to display font information in the dialog). This is true for all versions of AutoCAD I tested with; going back to AutoCAD 2013, but presumably it's also true for versions prior to that.

Thanks to Samir Bittar for bringing this issue to my attention. It seems the issue has also been discussed at some length on the discussion forums. Within the AutoCAD Engineering team, the issue has been researched very thoroughly by Arthur Ding, an engineer based in our Shanghai office. Arthur discovered how VS2013 works differently to VS2012 when debugging .NET modules and invalidates a core assumption made inside AutoCAD.

When VS2013 launches AutoCAD – at least via the debugging code-path that enables Edit & Continue – an additional thread gets created and is used to load the basic modules – such as acdb*.dll – into the process before exiting. With VS2012 this is not the case: it's AutoCAD's main thread that loads the basic modules, just as it would when run outside of a debugger. Our AcDb module naturally expects to have been loaded by AutoCAD's main thread and so caches the loading thread's ID for later use.

The main place the issue is currently encountered is when accessing certain fonts: the loading fails for these fonts as the load attempt is made from a thread that AcDb doesn't recognise as being the main one (and for safety's sake this is disallowed by AcDb – it isn't thread-safe and so prevents access to database operations from an arbitrary thread). When the font is then accessed – for instance in the STYLE dialog or the MTEXT editor – this either leads to (at best) an error or (at worst) a crash.

The good news is that there are two simple approaches that can be used to avoid this problem using VS2013, either of which should stop AutoCAD from crashing (due to this particular problem, anyway).

  1. Turn on "Use Managed Compatibility Mode" via Tools –> Options –> Debugging.
  2. Turn on "Enable native code debugging" from Project –> Properties –> Debug.

The latter one comes with a performance penalty, as it obviously has more work to do paying attention to the unmanaged code in AutoCAD and the underlying OS (of which there is a fair bit).

The bad news is that both of these two options disable Edit & Continue for 64-bit applications. So while it's possible to make use of VS2013 to debug 64-bit .NET modules using one of the above approaches, you won't be able to make use of one of VS2013's most eagerly-awaited new features while doing so.

[It seems there are also issues when debugging Revit with VS2013. See this post on Jeremy's blog for more information on that.]

30 responses to “Debugging AutoCAD using Visual Studio 2013”

  1. Kean,
    Does this mean Edit & Continue will work on 64bit inside AutoCAD as long as you aren't trying to access fonts? Or is it just a waste of time to attempt Edit & Continue?

  2. Mike,

    The specific problem accessing fonts is likely to be the tip of the iceberg: the fact that the wrong thread loaded AcDb is likely to cause all kinds of issues, some subtle, some much less so.

    You'd risk spend more time tracking down strange behaviour than you'd end up gaining from E&C, I suspect.

    Regards,

    Kean

  3. Hi Kean:

    Those are bad news, I wonder if there is any chance to fix the thread issue in the AcDb*.dll (i found only a few of them in my 2014 install) with a set of new ones and replacing the existent just for development?

    Regards,

    Gaston Nunez

  4. Hi Gaston,

    It is indeed bad news (if you were counting on being able to use Edit & Continue, of course).

    We're considering our options, but this new debugging mechanism presents quite a big structural change (however simple it might appear).

    If you're an ADN member you might want to check in with that team - they should certainly be tracking the significance of this issue with external developers.

    Regards,

    Kean

  5. Great post, I works great with the "managed compatibility mode" you mentioned! Thanks!

  6. Thanks to you for raising the issue, Samir. 🙂

    Kean

  7. Congrats Samir!

    Thanks a lot!

  8. I just wish acad did not take so long to start up when debugging. I never had great luck with e&c anyway, the errors caught were not usually fixed with minor tweaks, I needed to change some class definition or something.

  9. Hi Kean,

    did you also try this scenario?:
    1. Start AutoCAD regularly
    2. "netload" the .NET dll
    3. In VS 2013, use "Attach to process" (or however it's called in english VS editions)
    4. Hit a break point and try to edit and continue

    I haven't tested it myself as I'm still using VS2010 but maybe it works.
    This also improves AutoCAD speed while debugging massively btw.

    Best Regards

    Christian

  10. Hi Christian,

    It's indeed called "Attach to Process". 🙂

    I haven't tried it myself - I'm also not in front of my system with VS2013, at the moment - but the indications from the post about Revit aren't good (if I've understood the situation correctly).

    I'll give it a try, later on, although I don't expect it to work (I'll post a follow-up comment if it happens to).

    Regards,

    Kean

  11. plus.google.com/10133818587113 Avatar
    plus.google.com/10133818587113

    I stumbled upon this question maybe an hour after reading this plog post yesterday. Funny how the timing worked out, but looks like you saved another development soul from debugging woes:

    http://stackoverflow.com/questions/20201600/autocad-undefined-shape-style-dialog-crashes

  12. Good to know - thanks for spreading the word.

    Even if it isn't perfect (i.e. E&C isn't working) at least there's a way to make it work.

    Kean

  13. Sure enough, it fails. Attempting to edit code causes a messagebox to appear with the title "Edit & Continue" and the following message text:

    >>>
    Changes are not allowed in the following cases:

    - When the debugger has been attached to an already running process.
    - The code being debugged was optimized at build or run time.
    - The assembly being debugged is loaded as domain-neutral.
    - The assembly being debugged was loaded through reflection.
    - When Intellitrace events and call information is enabled.
    <<<

    Oh well - thanks for the suggestion, anyway. 🙂

    Kean

  14. Hi Kean,

    too bad, but thanks for testing it. It was worth a shot.

    On a more general note, I'd like to thank you for your blog posts that are immensly helpful to us AutoCAD developers! Whenever I need some info, I first check your blog and usually I find what I was looking for 🙂
    Keep up the great work!

    Best Regards

    Christian

  15. Hi Kean,

    (Neither IE nor Chrome let me reply to one of your answers, so I'm posting it here, sorry..)

    too bad about "Attach to process", but thanks for testing it. It was worth a shot.

    On a more general note, I'd like to thank you for your blog posts that are immensly helpful to us AutoCAD developers! Whenever I need some info, I first check your blog and usually I find what I was looking for 🙂
    Keep up the great work!

    Best Regards

    Christian

  16. Hi Christian,

    Thanks for the kind words - it's my pleasure. 🙂

    Regards,

    Kean

  17. Hello Kean,

    The first two options you recommended did not work for me. However, this is what did:

    Right-click on "acad" project in the solution explorer -> properties -> "Use Legacy Managed Engine"

    Note that my initial project setup needed to take advantage of the "Solution 3" you recommended in another blog:
    keanw.com/2010/04/hitting-breakpoints-in-net-class-libraries-while-debugging-with-visual-studio-2010.html

    Regards,
    Jason

  18. Hi Jason,

    Ah, so this is how you need to do it for a solution containing a pre-built EXE... good to know.

    Thanks for the info,

    Kean

  19. Sorry I couldn't quite follow the entire post & comments. Was this using VS 2013 and AutoCAD 2013 or AutoCAD 2014?

  20. VS 2013 and either AutoCAD 2013 or 2014...

    Kean

  21. Hi Christian,

    It's my pleasure! Sorry for the delay in replying - this (and your previous) comment ended up in my spam folder, for some unknown reason.

    Thanks for the feedback,

    Kean

  22. I have developed a dirty patch and tested it with ACAD2014 x64 (Ger), so far it looks good to me:
    open acdb19.dll with a hex editor and search for the byte sequence:
    0F 85 CF 80 53 00
    replace it with
    90 90 90 90 90 90
    please make a backup of acdb19.dll before, only change the bytes if the exact byte sequence is found and that uniquely
    This patch replaces the conditional jump with a no jump -> thread id is not tested any more by acdb19.dll

    1. Any ideas on how to patch version 2012 x64? The byte sequence suggested is not found in that version.

      1. Sorry - no idea. This approach really isn't recommended (aside from contravening the license agreement).

        Kean

        1. I understand. Although this was the approach recommended from one of the autodesk developers when I asked this question at au a few weeks ago. He did suggest a new version autocad first.

  23. Thanks for sharing this, Thomas, although it clearly contravenes our EULA and is unsupportable by Autodesk.

    We're looking into what we can do to address this issue in a supportable way.

    Kean

  24. Another issue with using option 1 or 2 is the error messages will show, in the immediate window, that were suppressed by editing the acad.exe.config file.

  25. Hi Kean, I was trying to you launch AutoCAD 2013 from VS2013 in Windows 7 to debug a 64-bit class library. Code was created on VS2012 but I had to upgrade because after some Windows OS updates, VS2012 just no longer worked. AutoCAD does not crash but any text o mtext created by the code was no longer appearing in model space. I tried the first approach and it works. Thanks a lot!

  26. Just switched vom VS2010 to VS2015 ,and was looking for the Reason for those "Undefined Symbol" messages comming up while debugging.

    Thanks for posting the Solution.

    Is this Problem solved in the newer AutoCAD Versions? (I'm using 2014 at the moment)

  27. Not sure if it's exactly the same problem... the Edit & Continue problem was fixed with AutoCAD 2015, at least:

    keanw.com/2014/03/autocad-2015-for-developers.html

    Kean

Leave a Reply to Gaston Nunez Cancel reply

Your email address will not be published. Required fields are marked *