24

Periodically questions are posted where the OP uses a property in its getter/setter which leads to recursive calls and results in StackOverflowException. The most recent one is here.

How should we handle such questions?

In my opinion, these are typos and have to be closed: instead of typing the backing field name, the OP has typed the property name. I guess every programmer has to face this error once. Some of them (those hopeless kind) will ask a question on Stack Overflow, and it means there will be hundreds of such questions, not linked, not shown as relevant, etc.

Closing a question as duplicate is an option too. We have a canonical duplicate for NullReferenceException), but I am unable to find one for StackOverflowException.

Yet, every such question (at least from my experience) receives an answer, which in my opinion is wrong.

13
  • For this particular one I would Have choose : stackoverflow.com/questions/1437791/…, stackoverflow.com/questions/3276156/… Commented Nov 5, 2020 at 8:41
  • 23
    Shouldn't those be asked on meta? /s
    – yivi
    Commented Nov 5, 2020 at 8:44
  • 5
    Hm. StackOverflowException could refer to at least two wildly different scenarios; it is the result of infinite recursion, but it can also be the result of an actual stack-like collection overflowing. Infinite recursion is very likely the scenario we are talking about here, so in my opinion an answer which is about recursion and covers the topic of infinite recursion could service as a canonical.
    – Gimby
    Commented Nov 5, 2020 at 9:13
  • @Gimby, I am not aware about second scenario, do you mean collection itself will throw StackOverflowException in one of its methods or what? Do you have an example?
    – Sinatr
    Commented Nov 5, 2020 at 9:28
  • @Sinatr My point was that this is about (infinite) recursion, not specifically about a StackOverflowException.
    – Gimby
    Commented Nov 5, 2020 at 9:40
  • 3
    Are you talking about the specific one (StackOverflowException - C# only? Java too?) or stack overflow exceptions in general? Or even broader, stack overflows in general (e.g. undefined behaviour in (stone-age) C)? Commented Nov 5, 2020 at 22:24
  • 1
    @PeterMortensen, thanks for fixing grammar and videos. Lundin has already correctly identified my question as C# specific in his answer. But maybe language-agnostic duplicate is not a bad idea? What if language specific answers can be then posted there with specific common mistakes?
    – Sinatr
    Commented Nov 6, 2020 at 9:07
  • Should this be just a canonical for the specific case of recursively-called getter/setter only? Or all kinds of stack overflow (perhaps it can explain what stack overflow is, but would there be a sizable component who already knows what's it already)?
    – user202729
    Commented Nov 6, 2020 at 9:32
  • 3
    Wow, it me a long time reading through the question and comments to realise there was no connection between "StackOverflowException" and the name of this site. Commented Nov 6, 2020 at 13:20
  • 2
    I wonder: If you create a stack overflow as a programmer, and you are looking for help, does it surprise that you may end up at a site named stackoverflow.com? I mean one could answer in this sense: "While stackoverflow is here to help for any kind of programming problems, stack overflows are the consequence of unwanted direct or indirect recursion most of the time". And we'd need one good answer how to avoid unwanted recursion and how to do intended recursion right.
    – U. Windl
    Commented Nov 6, 2020 at 13:44
  • 6
    I once had a stack overflow issue and I tried to search first, like a good person. Alas... However useful this site may be, it made it impossible to find the answer to that particular problem.
    – GolezTrol
    Commented Nov 6, 2020 at 15:11
  • 1
    Your example is very specific to C#, most languages don't have properties. In other languages, people tend to notice trivial endless recursions a bit more.
    – Passer By
    Commented Nov 7, 2020 at 1:24
  • And here I am, not knowing anything about stack overflow the programming problem, thinking StackOverflowException is some kind of April fool's joke or something related to Stack Overflow the website.
    – Nuclear241
    Commented Nov 7, 2020 at 14:24

1 Answer 1

25

Have the C# community settle for a canonical dupe. As mentioned in comments, these look like potentially good candidates:

Overloading getter and setter causes a stack overflow in C#
Stack overflow exception in C# setter
(Maybe edit the titles of these questions into shape, review tags and so on.)

Or if no good-enough canonical dupe exists, then make one:

  • Post it as a self-answered community wiki.
  • (You can flag the question to have both Q + A set as community wiki by a diamond mod)
  • You could even post a draft here on meta and have it peer reviewed in advance, preferably by a number of C# gurus/gold badge holders.

Once settled which one to use as canonical dupe, add a link to it below the FAQ at the tag wiki, for easy access by other user moderators.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .