BLOG - Filter: CSharp
Different behavior in Debug and Release version of a .NET application - How to find the bug
Samstag, 06. Dezember 2008, Christoph Ertl

binary conflict A few days ago I had a problem at a .NET 1.1 project where the release version of the application had a different behavior than the debug version. I felt like in the old days when developing with C++ and struggling through precompiler instructions.

The beginning

Tried to reproduce the bug with a Unit test which was not possible. Had a detailed look at the production data and refined the unit test. No success. At this point I had the suspicion that this could' be a debug vs. release problem. Tried the unit test in release mode and the test failed.

OK. There's a difference but how to find out where and what is the difference? And if found, why is there a difference?

Where's the difference?

Even with the information from the unit test it's not easy to find out why there is a difference. First of all we have to find the location of the difference. To find the location I used a profiler while running the unit test in debug and in release mode.

Then I compared the two call stacks and within seconds I got the method where the difference was located.

image 
callstack (orderd by execution time) as expected in the debug execution.

 

image
different callstack (ordered by execution time) in the release execution.

What's the difference?

Now we know the method were the difference is located. The next step is to compare the code of the release and debug assembly. This is best done using Redgate's .NET reflector.

Comparing the methods is an easy task with .NET Reflector. If your method is too large you can use an add-in which supports comparing two assemblies.

Why is there a difference?

While comparing the two version of the code you see in detail what's different. But why?

The difference can be caused by some reasons:

  1. Removed code in release assembly because of the Conditional attribute.
    Check the method which is missing in the callstack if it is marked with this attribute.
  2. The difference can also be caused by a compiler optimization like inlining etc. but this should not lead to a different behavior unless there is an
  3. Error in the compiler
    In this case you must try to change your code and see what's the result. A detailed inspection and understanding of the differences can help.

The found bug

For the sake of completeness the details about the bug I found. This bug only arises with the compiler shipped with Visual Studio .NET 2003. With Visual Studio .NET 2005/2008 there were no problems.

The code written in the editor:

private static void DoIt(bool flag) {
 
if(flag) {
   
using(MyDisposable d = DoItNormal()) {
   
}
 
}
 
else {
   
DoItDifferent();
 
}
}

The debug version inspected with .NET Reflector:

Debug assembly inspected

The release version inspected with .NET Reflector:

Release assembly inspected

Removing the empty using block and calling Dispose() explicitly solves the problem.

References

Tag cloud for Umbraco CMS blog package
Sonntag, 08. Juni 2008, Christoph Ertl

A view weeks ago I created the list of categories for my blog. After a while I was wondering if a tag cloud wouldn't be more intuitive and more fancy. Of course it is.

So I started to create my first Umbraco package. But before creating the package the cloud itself must be implemented.

The Tag Cloud

To get an idea what a tag cloud is and what it's based on have a look at Wikipedia.

Algorithm

There are a lot of posts and notes about tag clouds and how to calculate the weight of the tags. After reading a lot of these articles and trying some solutions myself I kept these two posts in mind.

As a result I use an logarithmic algorithm which should be fast enough and the quality of the result should be ok. Most of the posts are talking about calculating the font size, but I just calculate the weight of the tag. The font size (or color) itself is provided using stylesheets which provides a very wide range of displaying the tag cloud.

Appearance

As already mentioned this package only calculates the weight. So the appearance can be set using stylesheets. In this context the color, font size and weight are interesting. To get a usable tag cloud some points must be considered:

  • The different weights must be visual distinguishable.
  • At least when moving the mouse over a tag it should get an underline to see what's the whole name of the tag (See "ASP.NET Dynamic Data").
  • The tags should appear in alphabetical order which is done automatically.

Tag Cloud sample 
Example of one tag cloud (6 weights) using different appearances. Just fonts, Just color and a combination where 3 font sizes and 6 colors are used.

The Package

The Umbraco package is a very simple package containing

  • the dll with the calculation and rendering logic
  • an xslt file called TagCloud.xslt (at least the link to the aspx page must be corrected in this file)
  • a macro called TagCloud

Usage

As you can see in the XSLT file which is part of the package just call the Method RenderTags().

string RenderTags(XPathNodeIterator nodes, string propertyAlias, string entryPattern, int nrOfWeightRanges)

  • nodes: The nodes containing the tags to count.
  • propertyAlias: The alias of the dynamic property containing the tags. In the default installation of the blog package the alias is "categories".
  • entryPattern: The pattern used for rendering the tag. 
    Sample: <a href="blog.aspx?filterBy={0}" class="tc{1}" title="{2}">{0}</a>
    There are three placeholders available:
    • 0: The tag text (used for link text and the url)
    • 1: The calculated weight (here used for setting the stylesheet class)
    • 2: The amount of occurrence (here used for displaying as tooltip)
  • nrOfWeightRanges: With this parameter you can set how many weight ranges should be used. Having the problem with the distinguishableness in mind this should be at most a value of 5 or 6.

To get the appearance working you have to add classes for each weight in the stylesheet according to the number of weight ranges.

a.tc1 { font-size:100%; text-decoration:none; }
a.tc2 { font-size:110%; text-decoration:none; }
a.tc1:hover, a.tc2:hover { text-decoration:underline; }
...

Download

You can download the TagCloud Package here.

Usage outside of Umbraco

This package is not restricted to be used within Umbraco. Just add a reference to TagCloud.dll into your project and use the TagCloudCalculator class.

// create an instance of the TagCloudCalculator class
// passing the nrOfWeightRanges as parameter

TagCloudCalculator
cloud = new TagCloudCalculator(6);
foreach(string tag in myTags) {
 
cloud.Count(tag);
  // use this call if you already know the count
 
// cloud.Count(tag, count);
}
string html = cloud.Render(@"<a href=""blog.aspx?filterBy={0}"" class=""tc{1}"">{0}</a>");