Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - System.StackOverflowException with Lambda

Under what situations would this code error out with System.StackOverflowException?

Accounts.Sort((x, y) => string.Compare(x.AccountId, y.AccountId));

Update:
the property is written as:

    public string AccountId
    {
        get { return _accountId; }
        set { _accountId = value; }
    }

Nothing special going on at all. Sort is not overridden either.

like image 980
BuddyJoe Avatar asked May 06 '10 20:05

BuddyJoe


2 Answers

Look at the callstack and you will see which function is executed over and over again. If this is not possible (because it is running inside a production environment for example), give more info.

about what the called properties call, where this function is called, etc

like image 64
Henri Avatar answered Oct 09 '22 04:10

Henri


If AccountId does something non-trivial (anything except access a local field) then that is the most likely bet.

An interesting fact is that technically Sort requires the ordering to be transitive, and string comparison is not always transitive! But this would rarely cause a stackoverflow (unless the Sort method uses some kind of FP approach); it might cause it to run forever, but I believe the inbuilt types even have this covered (they check for the theoretical max run-length and abort, IIRC).

I would be looking at AccountId; if it does something "clever" (like lazy-load some value from a parent collection), odds are the bug is in the "clever".

like image 44
Marc Gravell Avatar answered Oct 09 '22 03:10

Marc Gravell