Saturday, 17 September 2011

Bug Hunt: Chasing an obscure AccessViolationException (Silverlight)

So, I’m working on my graduation project, using Silverlight 4, Prism and the MVVM pattern.

Bug Hunting

Now, for some context on my bug:


A couple of days ago, I started implementing support for comments in my application, following the next steps:
  1. I added the necessary entity and relationships to my Entity Data Model.
  2. I added the corresponding CRUD operations in my domain service.
  3. I implemented the ViewModel (ViewCommentsViewModel), and a DataTemplate as my View (ViewCommentsWindowTemplate)
  4. I added a button in the view that has to show comments, and wired its Command property to “ViewCommentsCommand” in the corresponding ViewModel, and added an InteractionRequestTrigger to it, which is bound to an InteractionRequest on the ViewModel (ViewCommentsRequest), and shows a ChildWindow.
    here’s the XAML for this:

    image
  5. The ViewCommentsCommand’s “Execute” method retrieves the comments from the server, and “Raises” the ViewCommentsRequest.

All this worked great in this particular view. However, as I wanted to add the same functionality to other parts of my project, I suddenly started getting this weird problem right after the Comments are loaded, and the InteractionRequest.Raise is called:

image

Now, at this stage I FREAKED OUT!! what would you make of that?! And since this is encapsulating a COM error, you’ll have no stack trace or other interesting information.

When I continue execution, I get the following error, which isn’t any better!

image

So, what do I do? I google that with bing! and search for it on StackOverFlow (amazing Q&A site, by the way). But that didn’t help much. This seemed to be a problem that happens randomly, and in very distinct situations. There were some people suggesting to reinstall the Silverlight 4 tools, which I did (and I even reinstalled Prism), with no avail!

At this point, I had already put some breakpoints in my code in the Execute method for the ViewCommentsCommand. After following the execution of the program, I figured out that the ViewCommentsViewModel which I passed to the Raise method is instantiated correctly, and the Raise method returns without a problem. Therefore, my mind went into the DataTemplate, since I knew that sometimes messing up something in XAML may cause some of these obscure problems.

True enough, just as I glanced over my template: Eureka ! I found it! and you’ll never guess what it was. Here’s a snapshot:

image

WHAT! who would set a Height to “1(5)0”! what would that mean! and how could that even happen!

But I knew! I had that Height set to “200” at some point, and it seemed bigger than I wanted to. I “tried” to set it to “150”, by selecting the 2 and replacing it by 1, then selecting the first 0 and hitting “5”. Now, my keyboard is in AZERTY layout, so I should have hit “shift+5”. And to make matters worse, I had set a keyboard binding of a feature of CodeRush, that puts the selected text between parentheses, to use the “5” keystroke!
However, this should’ve been “1(0)0”! How this became what it was, I cannot account for!
Needless to say, removing the cursed parentheses solved the problem! (yaay!) and as I felt so ashamed of myself, I decided to blog about this, hoping that it might be of value to somebody (or to a future me who might get the same problem) :)
Takeaways:
  • Make sure you NEVER mess stuff in your XAML. EVER. While waiting for more features for showing errors in the editor (*cough* VS2011 *cough*), always check (and re-check) your bindings and property setters.
  • CodeRush is an AWESOME tool (check my previous post ;) ), but you can shoot yourself in the foot if you’re not careful. Be aware of that Embed Selection feature, and check your keyboard bindings (in DevExpress Options, go to IDE=>Shortcuts, then Selection=>Embedding).
  • When you encounter a weird error, It’s not the universe conspiring against you. You have DEFINITELY messed up somewhere :P.
  • And … However weird the bug might appear, there’s always a “Bug Hunt” for it ;)

1 comment: