I was programming a class which recursively instantiates itself in order to parse a XSD into another type of XSD. It downloads it from internet and walks through each element and if one of the elements has a complexType which is located in another URI then it simply does the same thing by creating one of the instance of itself.
But when the XSD is so big and has many external links, today my class crashed because of the Stackoverflow exception.
I decided to put some of the instance methods to another class (Helper.cs) as static members and now call them from within the parser class.
But I wonder if this is a good solution and can solve the Stackoverflow exception? I know how the instance methods are put into the stack frames but Will the static methods be treated in the same way? Will it cause any Stackoverflow exception?
The only difference between instance methods and static methods is that instance methods take an additional hidden
thisparameter. (actually, instance methods can also bevirtual, but that doesn’t make any difference here)They are both equally vulnerable to stack overflow.
If you want to avoid stack overflows for large inputs, don’t use recursion.