Ankh and AccessViolationExceptions

Lately, I’ve been receiving a number of Ankh error reports that had an AccessViolationException. This was particularly weird because you cannot get that exception from safe managed code. AccessViolationException is the exception you get when you try to access memory you’re not allowed to access, usually because you are dereferencing an invalid pointer. Since safe managed code does not expose raw pointers, it’s not an exception you usually see in .NET development.

As it turned out, the exception came from a property accessor on an object from the Visual Studio automation model, a Project object. This is a COM object exposed to .NET and is implemented by the implementor of a specific project type, such as a C# project or a C++ project. Being a COM object, it can, of course, be implemented in a non-managed language, usually C++, which I suspect is the case here.

That answered the question of why such an exception could be caught inside Ankh, but it did not answer why we were seeing it in this particular case. It took a while for me to be able to reproduce it, but one submitter of an error report finally gave me a reproducible recipe: he had clicked on a linker error message in the Visual Studio error list. Linker errors aren’t associated with a specific source file, so clicking it usually does you no good, but in this case, it was clear that Ankh was doing something in response to this clicking and in the process, triggering the exception.

All files opened in Visual Studio belong to a project. This holds true even if you open a file that’s not part of your current solution, or if you don’t have a solution open. In that case, Visual Studio has a special project called "Miscellaneous Items", into which all files not part of any other project are put. This project is usually not shown in the solution explorer, but you can enable it by checking the "Show Miscellaneous files in Solution Explorer" checkbox in Tools->Options->Environment->Documents.

This project is not always present; rather, VS will load this project when the first "miscellanous" file is opened and close it again when no such files remain. Ankh has code to detect when a project is loaded and unloaded, and when a project is unloaded, we hook up an event sink to handle the case where files are added to or removed from the project, so we can refresh the status icons in the solution explorer. However, when such an event is raised, we cannot perform the refresh right away, because for some weird reason, the object model for the items hasn’t been updated to account for the changes yet. What we chose to do here is to schedule a delayed refresh of the item in question; ie, perform a refresh after a specific amount of time after the event.

What happened in this case was this:

  1. User clicked on the linker error in the error list.
  2. This, for some reason, triggered Visual Studio to load the Misc. Items project.
  3. Ankh then dutifully hooked itself up as an event sink for events on the Misc. Items project
  4. Visual Studio would then trigger the "Item added" event for the Misc. Items project (I’m not sure what it actually added, and don’t think I’ll spend much time trying to investigate it).
  5. Ankh would then schedule a delayed refresh of the project.
  6. Visual Studio would then, probably because it suddenly realized there was nothing to show, unload the Misc Items project again.
  7. Ankh would unhook itself as an event sink for the Misc Items project.
  8. However, the delayed refresh would still fire after its given interval.
  9. The refresh would then work against a Project object representing a project that was no longer loaded. Accessing its properties at this time triggers the AccessViolationException.

The above sequence is a little simplified – Visual Studio did actually fire several Item Added and Item Removed events before unloading the Misc Items project, but it illustrates the problem.

One unanswered question here is why the COM/.NET infrastructure allowed the Project object to become invalid when there was still a reference to it in the managed heap. We’ve had a similar problem with ProjectItem objects, but in that case we got a more informative exception stating "ProjectItem not available".

Oh well, now that I know what the problem is, I only have to find a solution 🙂

This entry was posted in AnkhSVN. Bookmark the permalink.

16 Responses to Ankh and AccessViolationExceptions

  1. Unknown says:

    With the dust settling wow gold now on World of Warcraft, it is wow po becoming more and more obvious that this is by far the best MMORPG currently on buy cheap wow gold the market. buy cheap wow gold It manages to walk a line that wow gold makes the game appealing both to the hardened power gamer, and casual weekend wow gold warrior, where both cheap wow gold can enjoy the game in their own special way, without stepping on each other’s toes. cheap wow gold If you are looking for a new game wow gold kaufen in the genre, you would be doing yourself a terrible disservice by wow gold kaufen overlooking World of Warcraft.

  2. Unknown says:

    As players make their way through the quests wow gold and start leveling up, it quickly becomes wow gold kaufen clear just how well balanced World of Warcraft is at allowing people to decide whether they want to go solo, or group with others to get things done. wow gold This all comes down to how well balanced the different classes are in the game. wow gold kaufen It shouldn’t come as a surprise that something like a warrior, or a rogue, or a hunter in this game can handle themselves quite well going it alone in WoW, but it is very impressive to see magic users like the mages, and priests being able to hold their own while soloing (albeit with a little more difficulty). wow gold Even more impressive is that priests achat wow gold can become very viable damage dealers thanks to the way their talent tree is set up, a very welcome change from the traditional heal-bot nature that priest-like classes have suffered from in the past. wow po As one gets to the higher levels, more of the quests become something designated as "Elite" quests. achat wow gold These involve fighting particularly strong enemies, and will require grouping in order to complete them well. Cheapest wow gold It is a good way to make sure players are Cheapest wow gold well-trained when they hit the end game material, but for those who would prefer to solo all the way to level 60, there are still a decent number of soloable quests available

  3. Unknown says:

    The best headphones for mp3 players are the most important mp3 accessory for your mp3 player. mp3 player It isn’t really an accessory, as it is mp3 player required for you to enjoy its music. cheap mp3 player As such, you’ll want to get the best headphones for your mp3 players.Sometimes they are even disregarded. buy mp3 player People rather look at the colorful and shiny mp3 player itself, with its cheap wow power leveling leek design and attractive graphic display. mp3 player However, no matter if it’s the best buy mp3 player sounding mp3 player, the final and most important step between the music and your ears is left to the headphones. MP3 PLAYER kaufen Those are the sole elements responsible for translating the electric audio signals back into the air vibrations commonly known as sound.So, don’t go overboard and buy the most expensive mp3 player, and then stick with MP3 PLAYER kaufen some low quality headphones. mp3 player To truly experience mp3 player music any music you need to hear it right. That’s why you should get the best headphones for mp3 players you can find

  4. jade says:

    書法-粉彩-水墨畫班-文儀用品-文件架

  5. Unknown says:

    roulette free roulette systems roulette strategies how to play roulette online roulette Water Run your car on water Run Car on Water Water Powered Car Water Car horse racing horse racing tips horse racing betting horse racing software horse racing systems movies downloadable movies download full version movies download movies movie downloads forex forex killer forex trading education forex software learn forex world of warcraft warcraft world of warcraft cheats world of warcraft download world of warcraft hacks Poker online poker how to play poker learn poker poker online blackjack online blackjack blackjack strategy how to play blackjack iphone iphone downloads iphone games download iphone download site review iphone download sites

  6. Unknown says:

    威而柔,自慰套,自慰套,SM,充氣娃娃,充氣娃娃,潤滑液,飛機杯,按摩棒,跳蛋,性感睡衣
    威而柔,自慰套,自慰套,SM,充氣娃娃,充氣娃娃,潤滑液,飛機杯,按摩棒,跳蛋,性感睡衣
    情人歡愉用品,情人用品性哥,情惑用品性哥,視訊交友90739,情人視訊網,情人節禮物,情人用品性哥,情人歡愉用品,情色交友,視訊交友,辣妹視訊,美女視訊,aio交友愛情館
    免費視訊聊天,免費視訊聊天,辣妹視訊,視訊交友網,美女視訊,視訊交友

Leave a comment