The Wayback Machine - https://web.archive.org/web/20081209073901/http://sql.codeproject.com:80/Feature/SubtleBugs.aspx
5,713,088 members and growing! (235 online)
Announcements
* Bold indicates new messages since 21:39 8 Dec '08




BullFrog Power
Advanced Search
Sitemap

Subtle Bugs


RSS Feed RSS feed

Ever spent a week trying to find that elusive bug only to realise it was staring you in the face the whole time?

Subtle Bugs is a forum to post examples of interesting, aggravating and subtle bugs that you've found and fixed. Do not post programming questions in this forum. This forum is purely for amusement and discussions and all actual programming questions will be removed.

 Msgs 1 to 25 of 118 (Total in Forum: 2,237) (Refresh)FirstPrevNext
NewsC# Compiler bug or Subtle bug, I don't know, but this is certainly a doozy!memberSuper Lloyd4hrs 34mins ago 
I have a problem with Lambda expression, looks like a C# compiler bug to me....

This piece of code

static void Main(string[] args)   
{
var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark" };
var actions = new List<Action>();
foreach (var item in names)
actions.Add(() => Console.WriteLine(item));
foreach (var a in actions)
a();
}


Will print "Mark, Mark, Mark, Mark"
Instead of "Lloyd, Stuart, Gabe, Mark" as it should....

I can fix it with the slightly modified version below..

static void Main(string[] args)   
{
var names = new List<string> { "Lloyd", "Stuart", "Gabe", "Mark32" };
var actions = new List<Action>();
foreach (var item in names)
{
string s = item;
actions.Add(() => Console.WriteLine(s));
}
foreach (var a in actions)
a();
}



But I think it's a C# compiler bug...

A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.

JokeRe: C# Compiler bug or Subtle bug, I don't know, but this is certainly a doozy!memberPIEBALDconsult3hrs 58mins ago 
Yeah, it should print, "Chuck Norris,Chuck Norris,Chuck Norris,Chuck Norris". Big Grin
GeneralRe: C# Compiler bug or Subtle bug, I don't know, but this is certainly a doozy!memberSteve Hansen38mins ago 
ReSharper gives a warning about that. Let's just keep it at a feature.
Generalcontroller.v [modified]memberTodd Smith18:37 1 Dec '08  
It's always fun learning new technologies all at once such as ASP.NET MVC + Autofac (IoC container) + LinqToSQL and running into some bizzare exception such as the following:

The requested service 'controller.v' has not been registered


Lets see who can guess what the error is Big Grin


EDIT: here is the answer[^]

Todd Smith

modified on Tuesday, December 2, 2008 1:01 PM

GeneralRe: controller.vmemberleppie22:21 1 Dec '08  
Probably a bug in MVC, they have been actively introducing them.

xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 beta 1 - out now!
((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

GeneralRe: controller.vmemberThomas Weller6:34 2 Dec '08  
I don't know Autofac at all, but it sounds like a strange error message from deep inside of it.

I assume that because one can say that an IoC-container is about requesting previously registered services.
I don't think there is one (or should be) having this weird name. This sounds like part of the 'magic', or maybe it's something that LinqToSQL thinks it should have by the sheer law of nature... Smile

Regards
Thomas

www.thomas-weller.de

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.


GeneralRe: controller.vmemberPIEBALDconsult7:55 2 Dec '08  
Linq is unnatural.
GeneralRe: controller.vmemberTodd Smith8:01 2 Dec '08  
Thomas Weller wrote:
I assume that because one can say that an IoC-container is about requesting previously registered services.
I don't think there is one (or should be) having this weird name. This sounds like part of the 'magic', or maybe it's something that LinqToSQL thinks it should have by the sheer law of nature...


I had many similar thoughts on what it could be. I wasn't able to find it until I downloaded the source code to Autofac and ASP.NET MVC and was able to step through them.

It ended up being a copy'n'paste bug. src="v/images/search.gif" should have been something like src="../../Content/images/search.gif". The url /v/images/search.gif matches the default /controller/action/id pattern that MVC looks for. So Autofac was trying to construct a controller object named v which resulted in an obscure exception message.

Another good tip is to add routes.IgnoreRoute ("favicon.ico"); or you'll get an error on that one as well.

Todd Smith

GeneralC++: Reading an ifstreammemberPJ Arends10:09 1 Dec '08  
This bit of code has been running flawlessly for quite a while.

The data file is a text file that contains a bunch of white space separated integers that get read into a vector.
I allow comments in the data file by preceding the comment with a semicolon.

I always had revision history comments at the end of the file but yesterday I added a new one at the end and the whole app stopped working.

Can you see the problem?
std::vector<int> targets;
std::ifstream targetfile(FileName.c_str());
if (targetfile.is_open())
{
int number = 0;
targets.push_back(number); // place holder for position zero
while (!targetfile.eof())
{
char next = targetfile.peek();
if (next == ';')
{
// bypass the rest of the line as it is a comment
while (next != '\n')
{
targetfile.get(next);
}
}
else if (isspace(next))
{
// advance pass the white-space
targetfile.get(next);
}
else
{
// grab the number and add it to the vector
targetfile >> number;
targets.push_back(number);
}
}
targetfile.close();
}



You may be right
I may be crazy
-- Billy Joel --


Within you lies the power for good - Use it!

GeneralRe: C++: Reading an ifstreammemberdan neely10:23 1 Dec '08  
Your final line didn't end with a newline character?

Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall

GeneralRe: C++: Reading an ifstreammemberPJ Arends11:14 1 Dec '08  
Yeah, that was it. The solution was to check for the end of file while attempting to bypass the comment line.
// bypass the rest of the line as it is a comment
while (next != '\n' && !targetfile.eof())
{


Lesson: always check for EOF when reading a file in a loop.Sigh


You may be right
I may be crazy
-- Billy Joel --


Within you lies the power for good - Use it!

GeneralRe: C++: Reading an ifstreammemberWilliamSauron10:57 1 Dec '08  
A newline at the end of a comment is worth two in the bush.

-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com

S. L.

GeneralT-SQL: null != value evaluates false [modified]memberSyed Mehroz Alam1:59 14 Nov '08  
We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false.

Run the following T-SQL statements and you will get false for both the cases.
print (Case When null ='Value' Then 'true' else 'false' end)
print (Case When null!='Value' Then 'true' else 'false' end)


The query was something like:
Select ...
Case When Column1='SomeValue' .. Then ..
Case When Column1!='SomeValue' .. Then ..
..


I was wondering why none of case statements were evaluating true. Finally, I came to know Column1 resulted in a null due to a left join and then null!='SomeValue' returned false.


Syed Mehroz Alam

My Blog
My Articles

Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

modified on Friday, November 14, 2008 7:12 AM

GeneralRe: T-SQL: null != value evaluates falsememberJason Lepack (LeppyR64)3:38 14 Nov '08  
Syed Mehroz Alam wrote:
We all know that null, when compared with anything, returns false. Even null=null evaluates to false. But I was amazed to see that null!=Value also returns false.


!= is still a comparison operator.

Example:
I have two things on my desk. One is a baseball. Is the other thing a baseball? Is the other thing not a baseball? You don't know, right?

Now, I think it would have been more useful to have comparisons containing nulls return null, instead of false. But that is beside the point.
GeneralRe: T-SQL: null != value evaluates falsememberDag Oystein Johansen4:27 29 Nov '08  
You're certainly right about how it actually does behave. But I don't really see why comparions with null ought to behave differently in SQL than in, say, C#. This whole design decision of using the is operator for comparison to null seems illogical and counterintuitive, but I presume there is some reason for it. If you see a good reason, would you care to elaborate?
GeneralRe: T-SQL: null != value evaluates falsememberPIEBALDconsult3:47 14 Nov '08  
Syed Mehroz Alam wrote:
I was amazed


I wasn't.
GeneralRe: T-SQL: null != value evaluates falsememberScott Barbour6:00 14 Nov '08  
Which is why you should use the isnull() function when you want to compare a nullable field to a another field or literal, and field is null (or field is not null) when you want to check for null, though I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.

I don't claim to be a know it all, for I know that I am not...

I usually have an answer though.


GeneralRe: T-SQL: null != value evaluates falsememberSyed Mehroz Alam9:55 14 Nov '08  
You are right, I also tackled it using IsNull but the original query wasn't written by me and it was hard to find the cause of error since the query was a complex one.

My Blog
My Articles

Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

GeneralRe: T-SQL: null != value evaluates falsememberalejandrofuchs12:50 19 Nov '08  
Null = Value does not evaluate to false, nor true. It's Unknown.
So there are not only two values: True and False, but also Unknown

From MSDN:

A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown.
GeneralRe: T-SQL: null != value evaluates falsememberDag Oystein Johansen4:31 29 Nov '08  
Scott Barbour wrote:
I typically use isnull(field,'') <> '' as our users are wont to make fields empty strings.


If you do not want to represent two different things by null or empty string, why is the field nullable in the first place? In my view, using invariant representations is a virtue.

(Of course, there may be some cases where null and empty string is different for some purposed but not others, and then I guess this technique might be useful.)
GeneralRe: T-SQL: null != value evaluates falsememberScott Barbour10:44 29 Nov '08  
I didn't design the tables, I'm just tasked with working with them. If it has never had a value, it is null. If the value has been cleared, it is an empty string.

Of course, using the isnull(field,'') is even more useful when you are looking for specific values when nulls are present. It has been my experience with MS SQL Server 2000 that queries behave oddly with nulls (such as excluding rows from the resultset.) especially when using the LIKE operator.

Of course MSSQL 2000 has a number of quirks anyway. I've reworked a number of queries that just wouldn't return the correct data.

I don't claim to be a know it all, for I know that I am not...

I usually have an answer though.


Generalref keyword bad communication between .net languages!membermrcooll11:11 4 Nov '08  
i have discover that if u have two different project in same solution first is Presentation tear in C# and the other is BLL 'Business logic layer' tear in Vb.net have by ref Form parameter, you cant call it form c# by sending this as a parameter!


public sub SetFormPrivilage(by ref frm as Form)
''
'Some code goes here!
''
End sub
private SomeForm_load(object sender,EventArgs e)
{
SomeClassInOtherProjectObject Obj=new SomeClassInOtherProjectObject();
Obj.SetFormPrivilage(ref this);//Error you cant assign ref to this
//or
Obj.SetFormPrivilage(this);//Error SetFormPrivilage expect ref keyword


}
GeneralRe: ref keyword bad communication between .net languages!memberjamie55011:51 4 Nov '08  
Form blah = this;
Obj.SetFormrivilage(ref blah);

Of course, if blah changes, "this" will not. But that would be strange anyways

This also happens in pure C#, iirc, as it should
GeneralRe: ref keyword bad communication between .net languages!memberPIEBALDconsult11:55 4 Nov '08  
That's not a language problem, you're simply not allowed to pass this that way:

"
CS1605: Cannot pass '<this>' as a ref or out argument because it is read-only
"

And it's certainly not subtle.


In your case you probably don't need to pass by reference anyway.


Or you could do something like:


object o = this ;
Obj.SetFormPrivilage ( ref o ) ;

GeneralRe: ref keyword bad communication between .net languages!mvpCPallini23:54 4 Nov '08  
PIEBALDconsult wrote:
And it's certainly not subtle.


Big Grin

If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke

[My articles]

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Last Updated 8 Dec 2008
SQL | Advertise | Privacy
Copyright © CodeProject, 1999-2008
All Rights Reserved. Terms of Use