An Error of Judgement

I recently came to work and found that I had unwittingly coded a bizarre occurrence.  Unsure as to how I came across it, but thankfully it was spotted while I am still developing the solution.

image

The above error came as a result of this, this isn’t the error I saw in my application before spotting it, thankfully I spotted it before I built and test run the code.  But using a demo, this is the error I got, just to show you the result of this.  Below is the code that causes this;

class Program
{
	static void Main(string[] args)
	{
		TestClass tempClass = new TestClass();
	}
}

public class TestClass
{
	public int Id { get; set; }
	public string Name { get; set; }

	public TestClass()
	{
		LoadData();
	}

	public void LoadData()
	{
		for (int i = 0; i < 10; i++)
		{
			TestClass tempItem = new TestClass();
			tempItem.Id = i;
			tempitem.Name = string.Format("My Name - {0}", i);
		}
	}
}

It might not be completely obvious but there is an infinite loop there.  Not in the traditional sense which is why it might not seem too obvious.  In the Constructor of TestClass I call the method LoadData.  In which I create a new instance of TestClass, which them again calls LoadData and so on.

Like I said not your typical infinite loop;

while (true)
{
	i++;
}

Like above we can see that is will not exit this loop, but this one, will just cause the application to hang there.  The other ran into an error but the error, though correct might have you going down the wrong path.  Suffice to say I have fixed this, called the LoadData from the code path that creates the instance of the data, and the constructor is now empty.

Sometimes as programmers you have dumb moments, thankfully it got caught before it gets put into a production environment.

Advertisements

Posted on November 14, 2016, in Development and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: