Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2005 Aug : Stack overflow question

www.cryer.info
Managed Newsgroup Archive

Stack overflow question

Subject:Stack overflow question
Posted by:"Aaron" (1@2.com)
Date:Fri, 26 Aug 2005 08:52:39 +1200

Hi

Can anyone give me some advice on handling a stack overflow? I have a
program that is crashing without logging an error, and I suspect it is a
stack overflow (Dr Watson reported it at one place the software is being
used).
When I tried a simple program to see what happened when the stack
overflowed, it crashed in the exception handler. Some testing seemed to
imply that the stack is not restored to the context of the exception hanler
until after the exception handler was finished.
Can anyone suggest a way to usefully handle a stack overflow exception if
you can't do anything in the exception handler?


type
  TStackData = array[0..1024] of byte;


  TForm1 = class(TForm)
    ButtonCrash: TButton;
    procedure ButtonCrashClick(Sender: TObject);
  private
    { Private declarations }
    FStackCounter: Integer;
    procedure StackCrash(StackData: TStackData);
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.ButtonCrashClick(Sender: TObject);
var
  StackData: TStackData;
begin
  FStackCounter := 0;
  try
    StackCrash(StackData);
  except
    on E: Exception do
      begin
        ShowMessage(E.ClassName + ' in StackCrash: ' + E.Message); // <-
crashes here while handling the exception.
      end;
  end;
end;


procedure TForm1.StackCrash(StackData: TStackData);
var
  LocalStackData: TStackData;
begin // <- Raises stack overflow exception here
  Inc(FStackCounter);
  // Show how many calls deep we are
  OutputDebugString(PChar('Count: ' + IntToStr(FStackCounter)));
  StackCrash(LocalStackData);
end;


I'm using Delphi 5 on WinXPPro
--
Aaron

Replies:

www.cryer.info
Managed Newsgroup Archive