Friday 16 February 2007

.NET Properties – Infinite Loop

What's wrong with this code?

public bool IsModify
{
get
{
return IsModify;
}
set
{
IsModify = value;
}
}

The title of the post might give away the problem. This code will compile happily and any surrounding code will most likely execute without a problem—until the property is accessed. You have to look closely and compare the property name with the variable name but once you do, you'll realise they're identical; the result is get calling get on IsModify and so until a StackOverflowException is thrown—an infinite loop (the same thing occurs with the set).

What I failed to reveal in the code above is the variable name the property should be wrapping (note the casing):

private bool isModify;

Unfortunately, Visual Studio 2005 doesn't issue a warning about this potentially disastrous little bit of code.

What the problem boils down to is a difference in casing between the variable name and the property. My team's coding standards dictate this naming convention but I otherwise might have used _isModify for the local variable to identify it as such and differentiate it from its property name. On a style note, I find the underscores make the code a bit busy and my eye tends to jump to the variable names—which is probably inappropriate. Alternatively, I could give the variable a completely different name from the property name but that severs the implicit connection between the two.

This can result in all sorts of semi-intelligible error messages. Today, after waiting for the server to run its course, I got my old favourite:

Server Application Unavailable

The web application you are attempting to access on this web server is currently unavailable. Please hit the "Refresh" button in your web browser to retry your request.

Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server. Please review this log entry to discover what caused this error to occur.

The event log reveals two entries of interest:

Event Type: Error
Event Source: .NET Runtime 2.0 Error Reporting
Event Category: None
Event ID: 5000
Date: 16/02/2007
Time: 8:02:51 AM
User: N/A
Computer: PHO02078ITDEV
Description:
clr20r3, P1 aspnet_wp.exe, P2 2.0.50727.210, P3 45063b16, P4 app_web_nr0px690, P5 0.0.0.0, P6 45d4e6d7, P7 8, P8 0, P9 system.stackoverflowexception, P10 NIL.

and

Event Type: Error
Event Source: ASP.NET 2.0.50727.0
Event Category: None
Event ID: 1008
Date: 16/02/2007
Time: 8:02:59 AM
User: N/A
Computer: PHO02078ITDEV
Description:
aspnet_wp.exe (PID: 2168) was recycled because it failed to respond to ping message.

2 comments:

  1. The fact that you came across this in your code is pretty bad mate. Surely they taught you about this at Uni??? :)
    MB

    ReplyDelete
  2. Actually, this came up as a test question when I applied for a job a while back. With TWA's coding standard as it stands, it's also an easy mistake--especially with Intellisense "helping" out.

    The next version of the C# language should help with this by allowing properties to be declared without a backing variable. The compiler will automatically manage the backing variable on your behalf. Very handy--and less code.

    ReplyDelete

Spam comments will be deleted

Note: only a member of this blog may post a comment.