Board index » delphi » Stack overflow

Stack overflow

Quote
> I'm currently using TP7 for developing a larger DOS-Application.
> Now there is only one problem: I always get a stack overflow error if a special
> function is called. So I thought it was a problem with that function, but it is
> also called from within otherparts in the application and works then.
> This error also occurs when compiled for protected mode.
> I even tried increasing the stack per compiuler-option, but that didn't help...

   It's no doubt either (1) the nesting level at which you call this
subprogram and/or (2) the amount of local data declared and used in the
subprogram.  Either is possible from your problem statement.
   In (1), you may be calling the subprogram from the main line code,
where is works okay, but when you call it from Proc1 which is called by
ProcA (etc.) the total amount of local data used by all the invoked
subprograms may exceed the maximum Stack space reserved (in the $M
directive).  Note that the Stack limit controls the total combined amount
of local data used by all subprograms which are in execution at any given
moment, so the _level_ at which the problem subprogram may be the problem
here.
   In (2) (as well as (1)), you are probably declaring too much local
data in the problem subprogram, and that's where to attack both: you must
find ways to reduce the total data declarations (reduce the array
dimensions, the size of strings, etc.) or perhaps allocate large local
data structures on the Heap while the subprogram is executing (be sure to
release the Heap space when the logic terminates!).
   In summary, you have only so much Stack to use (64K max.), and it's a
shared resource - shared by all processing subprograms at any moment
they're in execution.  In complex programs, it's usually best to keep the
amount of local data used in subprograms to a minimum, because logic
nesting and recursion can easily lead to Stack Overflow errors.
 

Re:Stack overflow


Hi...

I'm currently using TP7 for developing a larger DOS-Application.
Now there is only one problem: I always get a stack overflow error if a special
function is called. So I thought it was a problem with that function, but it is
also called from within otherparts in the application and works then.
This error also occurs when compiled for protected mode.

I even tried increasing the stack per compiuler-option, but that didn't help...

Help is appreciated

-Oliver Albers

Re:Stack overflow


In article <20000122224507.23102.00000...@ng-fo1.aol.com>,

Quote
OAlbers <oalb...@aol.com> wrote:
>Hi...

>I'm currently using TP7 for developing a larger DOS-Application.
>Now there is only one problem: I always get a stack overflow error if a special
>function is called. So I thought it was a problem with that function, but it is
>also called from within otherparts in the application and works then.
>This error also occurs when compiled for protected mode.

>I even tried increasing the stack per compiuler-option, but that didn't help...

Stack overflow is caused typically either by uncontrolled (or very deep
recursion) or by defining large arrays in subroutines. If you use
recursion, check that it is controlled. Also avoid using functions that
return strings and string operations like "+" in recursive routines.
Instead create small non-recursive wrapper procedures that operate the
strings using variable parameters.

If you need arrays in subroutines, do not declare them directly (unless
you know it is safe) instead declare a pointer to the array and
allocate the pointer with new() and free it before exit with dispose().

If you use heap prepare for lack of it. This can be done best with
installing a heap error handler (see manual). Checking for maxavail
works in RM but not in PM.

Osmo

Re:Stack overflow


Tja,

Het zal je gebeuren!

Mike Copeland <mrc...@primenet.com> schreef in berichtnieuws
MPG.12f44c569274b0bf989...@news.primenet.com...

Quote
> > I'm currently using TP7 for developing a larger DOS-Application.
> > Now there is only one problem: I always get a stack overflow error if a
special
> > function is called. So I thought it was a problem with that function,
but it is
> > also called from within otherparts in the application and works then.
> > This error also occurs when compiled for protected mode.
> > I even tried increasing the stack per compiuler-option, but that didn't
help...

>    It's no doubt either (1) the nesting level at which you call this
> subprogram and/or (2) the amount of local data declared and used in the
> subprogram.  Either is possible from your problem statement.
>    In (1), you may be calling the subprogram from the main line code,
> where is works okay, but when you call it from Proc1 which is called by
> ProcA (etc.) the total amount of local data used by all the invoked
> subprograms may exceed the maximum Stack space reserved (in the $M
> directive).  Note that the Stack limit controls the total combined amount
> of local data used by all subprograms which are in execution at any given
> moment, so the _level_ at which the problem subprogram may be the problem
> here.
>    In (2) (as well as (1)), you are probably declaring too much local
> data in the problem subprogram, and that's where to attack both: you must
> find ways to reduce the total data declarations (reduce the array
> dimensions, the size of strings, etc.) or perhaps allocate large local
> data structures on the Heap while the subprogram is executing (be sure to
> release the Heap space when the logic terminates!).
>    In summary, you have only so much Stack to use (64K max.), and it's a
> shared resource - shared by all processing subprograms at any moment
> they're in execution.  In complex programs, it's usually best to keep the
> amount of local data used in subprograms to a minimum, because logic
> nesting and recursion can easily lead to Stack Overflow errors.

Other Threads