Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a stack overflow happen for any other reason that recursion?

I'm getting a stack overflow exception for a segment of code that doesn't seem to be able to produce a stackoverflow... It looks like this:

public String WriteToFile(XmlDocument pDoc, String pPath)
{
  string source = "";
  string seq = "";
  string sourcenet = "";

  XmlNodelist sourceNode = pDoc.GetElementsByTagName(XmlUtils.Nodes.Source);
  source = sourceNode.Item(0).InnerText;

  XmlNodelist sqList= pDoc.GetElementsByTagName(XmlUtils.Nodes.Seq);
  seq = sqList.Item(0).InnerText;

  XmlNodelist sourceNets = pDoc.GetElementsByTagName(XmlUtils.Nodes.SourceNets);
  sourcenet = sourceNets.Item(0).InnerText;

  string fileName = Folders.GetMyFileName(source, seq, sourcenet);
  string fullPath = Path.Combine(pPath, fileName);

  pDoc.Save(pFullPathFile);  <--- Stackoverflow is raised here

  return pFullPathFile; 
}

There are no recursive calls, if you examine the call stack it has a depth of 2 before going to "external code" (which I'm guessing is not that external but part of the framework that starts the thread, which has debugging turn off).

¿Is there anyway the exception can be risen because anything other than a recursive call? It does ALWAYS fails in the pDoc.Save method call... and pDoc isn't actually that big... more like 32KB of data...

like image 503
Jorge Córdoba Avatar asked Jan 23 '12 16:01

Jorge Córdoba


People also ask

Can stack overflow only happen with recursion?

The most-common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack. An example of infinite recursion in C.

What's stack overflow Why recursion may cause stack overflow and how do you prevent it?

If a program uses more memory space than the stack size then stack overflow will occur and can result in a program crash. If function recursively call itself infinite times then the stack is unable to store large number of local variables used by every function call and will result in overflow of stack.

What is the cause of stack overflow?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.

How does stack overflow handle recursion?

In recursion, the new function call will be created in stack memory section. If our recursive function doesn't have any base case or the recursive function call doesn't align towards the base case, it will keep on create function calls in the stack memory section.


1 Answers

A stack overflow exception can occur any time that the stack exceeds it's maximum size. This is mostly commonly done with by ...

  • Having a deeply nested stack which is not recursive. Think of event storms where event A leads to event B which leads to event C all of which have handlers that deeply grow the stack.
  • Having a shallow stack which occurs after some large stack allocations
like image 191
JaredPar Avatar answered Oct 21 '22 10:10

JaredPar