Board index » delphi » Unexpected heap overflow in protected mode program compiled by BP7

Unexpected heap overflow in protected mode program compiled by BP7

I use Borland Pascal 7.0 and comile protected mode SW.

Below simple program crashes with heap overflow error even when there is
enough DPMI memory available. Does anyone has similar experinece, is
there a workaround?

{$F+,O+} Program HeapTest; {$M 65520,0,655360}

Const Max_X=1650;

type
Ts=string[255];
TMatrix=array [0..Max_X] of ^Ts;

const ii=100;
var i,x:integer;M:array[1..ii] of ^TMatrix;

Begin
Writeln(MaxAvail,'  ',MemAvail);
for i:=1 to ii do
begin
new(M);
for x:=0 to Max_X do New(M^[x]);
Writeln(MaxAvail,'  ',MemAvail);
end;
Readln;
End.

--
Posted via http://dbforums.com

 

Re:Unexpected heap overflow in protected mode program compiled by BP7


Quote
gkomar wrote:

> I use Borland Pascal 7.0 and comile protected mode SW.

> Below simple program crashes with heap overflow error even when there is
> enough DPMI memory available. Does anyone has similar experinece, is
> there a workaround?

> {$F+,O+} Program HeapTest; {$M 65520,0,655360}

> Const Max_X=1650;

> type
> Ts=string[255];
> TMatrix=array [0..Max_X] of ^Ts;

> const ii=100;
> var i,x:integer;M:array[1..ii] of ^TMatrix;

> Begin
> Writeln(MaxAvail,'  ',MemAvail);
> for i:=1 to ii do
> begin
> new(M);

      ^Pointer variable expected.

Quote
> for x:=0 to Max_X do New(M^[x]);
> Writeln(MaxAvail,'  ',MemAvail);
> end;
> Readln;
> End.

With BP, you can allocate about 59330 Strings (max. 16MB RAM).

I guess your program is supposed to allocate about 42MB RAM. This is not
possible with BP, you might want to use newer compilers, such as
FreePascal or GNU-Pascal.

Wolf

Re:Unexpected heap overflow in protected mode program compiled by BP7


Dear Wolf,

It isn't about strings, sligthly modifying the source:
{$F+,O+} Program HeapTest; {$M 65520,0,655360}

Const Max_X=1650;

type
{ Ts=string[255];}
TMatrix=array [0..Max_X] of pointer{^Ts};

const ii=10000;
var i,x:integer;M:array[1..ii] of ^TMatrix;

Begin
Writeln(MaxAvail,'  ',MemAvail);
for i:=1 to ii do
begin
new(M);
{for x:=0 to Max_X do New(M^[x]);}
Writeln(MaxAvail,'  ',MemAvail);
end;
Readln;
End.

it still crashes. By the way it is only a small demo to demonstrate the
bug in BP, unfortunately I am stuck to BP with a huge program, which
would be very hard to port to other compiler.

Thanks for the reply though,
Gyula

--
Posted via http://dbforums.com

Re:Unexpected heap overflow in protected mode program compiled by BP7


Quote
In article <1806494.1031837...@dbforums.com>, gkomar wrote:

> it still crashes. By the way it is only a small demo to demonstrate the
> bug in BP, unfortunately I am stuck to BP with a huge program, which
> would be very hard to port to other compiler.

Just out of curiousity, what where the problems you encountered/expect with
e.g. FPC ?

Re:Unexpected heap overflow in protected mode program compiled by BP7


Quote
gkomar wrote:

> Dear Wolf,

> It isn't about strings, sligthly modifying the source:
> {$F+,O+} Program HeapTest; {$M 65520,0,655360}

> Const Max_X=1650;

> type
> { Ts=string[255];}
> TMatrix=array [0..Max_X] of pointer{^Ts};

> const ii=10000;
> var i,x:integer;M:array[1..ii] of ^TMatrix;

> Begin
> Writeln(MaxAvail,'  ',MemAvail);
> for i:=1 to ii do
> begin
> new(M);
> {for x:=0 to Max_X do New(M^[x]);}
> Writeln(MaxAvail,'  ',MemAvail);
> end;
> Readln;
> End.

> it still crashes. By the way it is only a small demo to demonstrate the
> bug in BP, unfortunately I am stuck to BP with a huge program, which
> would be very hard to port to other compiler.

This program still does _not_ compile (M is not a pointer, it is an
array)! So I can't reproduce a bug. What exactly is your problem? Can
you post a program that compiles? (then copy+paste, please)

If your problem is compiling:
type MatrixOfPMatrix=array[1..ii] of ^TMatrix;
var m: ^MatrixOfPMatrix;

Pascal is not C, an array is not a pointer!

Wolf

Re:Unexpected heap overflow in protected mode program compiled by BP7


"gkomar" <mem...@dbforums.com> schreef in bericht
news:1806095.1031830779@dbforums.com...

Quote

> I use Borland Pascal 7.0 and comile protected mode SW.

> Below simple program crashes with heap overflow error even when there
is
> enough DPMI memory available. Does anyone has similar experinece, is
> there a workaround?

OK here's the solution. (tested)
I had to modify you program a little for the faulty adressing of M[..]
But the secret is in
Hheaplimit and Heapblock
Now you can use 64 mb of memory.

--
Femme

{$F+,O+} Program HeapTest; {$M 65520,0,655360}

Const Max_X=1650;

type
Ts=string[255];
TMatrix=array [0..Max_X] of ^Ts;

const ii=100;
var i,x:integer;
 M:array[1..ii] of ^TMatrix;

Begin
  Heaplimit:=1024*8;
  Heapblock:=1024*48;
Writeln(MaxAvail,'  ',MemAvail);
for i:=1 to ii do
begin
  new(M[i]);
  for x:=0 to Max_X do New(M[i]^[x]);
Writeln(i:3,':',MaxAvail,'  ',MemAvail);
end;
Readln;
End.

Re:Unexpected heap overflow in protected mode program compiled by BP7


Wolf,

Of course it compiles. Yes M is an array not a pointer, but every array
member of M is a pointer (M:array[1..ii] of ^TMatrix;) so New(M) is a
valid operation. And I do know the difference between Pascal and C.

My problem is that it crashes even though I still have DPMI memory
according to MemAvail or MaxAvail, so it is a bug of BP.

Marco,

In the program that I encountered this bug needs a lot of memory to be
accessed by pointers. It can keep up to some point, but then randomly it
crashes with heap overflow.

Gyula

--
Posted via http://dbforums.com

Re:Unexpected heap overflow in protected mode program compiled by BP7


Quote
In article <1807915.1031856...@dbforums.com>, gkomar wrote:

> Wolf,

> Of course it compiles. Yes M is an array not a pointer, but every array
> member of M is a pointer (M:array[1..ii] of ^TMatrix;) so New(M) is a
> valid operation. And I do know the difference between Pascal and C.

Hmm, it shouldn't be. M is the array, and that isn't a pointer type.
FPC doesn't eat it (can't BP under *nix),

Quote
> My problem is that it crashes even though I still have DPMI memory
> according to MemAvail or MaxAvail, so it is a bug of BP.

Not really, it is a flaw of the 286 DPMI system, but it can be worked around
by the RTL. Because memory was expensive in '92 (release date BP), the
default values are just somewhat conservative.

Quote
> Marco,

> In the program that I encountered this bug needs a lot of memory to be
> accessed by pointers. It can keep up to some point, but then randomly it
> crashes with heap overflow.

This is probably due to the maximal selector limit, and one can stall the
problem till 64 MB, by the methods Femme posted, which has been discussed
and tested before.

But that wasn't the question. The question is why a different compiler
couldn't compile your program :-)

Re:Unexpected heap overflow in protected mode program compiled by BP7


"Marco van de Voort" <mar...@toad.stack.nl> schreef in bericht
news:slrnao1uaf.2p7i.marcov@toad.stack.nl...

Quote
> In article <1807915.1031856...@dbforums.com>, gkomar wrote:

> > Wolf,

> > Of course it compiles. Yes M is an array not a pointer, but every
array
> > member of M is a pointer (M:array[1..ii] of ^TMatrix;) so New(M) is a
> > valid operation. And I do know the difference between Pascal and C.

> Hmm, it shouldn't be. M is the array, and that isn't a pointer type.
> FPC doesn't eat it (can't BP under *nix),

Very strange indeed. When I tried to compile the program in BP 7, it
refused to compile
New(M); since M is not a pointer variable.

Quote
> > My problem is that it crashes even though I still have DPMI memory
> > according to MemAvail or MaxAvail, so it is a bug of BP.

Not a bug. Problem is that the program is running out of selectors. See
the solution in my other posting.

Quote
> Not really, it is a flaw of the 286 DPMI system, but it can be worked
around
> by the RTL. Because memory was expensive in '92 (release date BP), the
> default values are just somewhat conservative.

> > Marco,

> > In the program that I encountered this bug needs a lot of memory to
be
> > accessed by pointers. It can keep up to some point, but then randomly
it
> > crashes with heap overflow.

Be careful. Memavail and Maxavail do not report the correct amount of
available memory. Not a BP bug, but a Windows feature [;-}

Quote

> This is probably due to the maximal selector limit, and one can stall
the
> problem till 64 MB, by the methods Femme posted, which has been
discussed
> and tested before.

> But that wasn't the question. The question is why a different compiler
> couldn't compile your program :-)

Huh, what compiler?

Femme

Re:Unexpected heap overflow in protected mode program compiled by BP7


Quote
>This is probably due to the maximal selector limit, and one can stall
>the problem

till 64 MB, by the methods Femme posted, which has been discussed and
tested before.

I am not able to find these discussions, can you direct me how to locate
them, I am very interested.

Quote
>But that wasn't the question. The question is why a different
>compiler couldn't

compile your program :-)

I tried to use Free Pascal, but my program does extensive hardware
access and has some assembler as well. There are some differencies as
far as I remember from BP. So I think it could be compiled, but it would
be a lot of work, and a lot more testing.

--
Posted via http://dbforums.com

Re:Unexpected heap overflow in protected mode program compiled by BP7


Quote
In article <1810086.1031901...@dbforums.com>, gkomar wrote:

>>This is probably due to the maximal selector limit, and one can stall
>>the problem
> till 64 MB, by the methods Femme posted, which has been discussed and
> tested before.

> I am not able to find these discussions, can you direct me how to locate
> them, I am very interested.

See the example that Femme posted two days ago, and search for "heapblock" in
this group on groups.google.com

Quote
>>But that wasn't the question. The question is why a different
>>compiler couldn't
> compile your program :-)

> I tried to use Free Pascal, but my program does extensive hardware
> access and has some assembler as well. There are some differencies as
> far as I remember from BP. So I think it could be compiled, but it would
> be a lot of work, and a lot more testing.

True. But if it is somewhat localized, it often isn't that bad, since it
can remain Dos in first approx.

Re:Unexpected heap overflow in protected mode program compiled by BP7


Thanks Marco

--
Posted via http://dbforums.com

Re:Unexpected heap overflow in protected mode program compiled by BP7


"gkomar" <mem...@dbforums.com> schreef in bericht
news:1810086.1031901800@dbforums.com...

Quote

> >This is probably due to the maximal selector limit, and one can stall
> >the problem
> till 64 MB, by the methods Femme posted, which has been discussed and
> tested before.

> I am not able to find these discussions, can you direct me how to
locate
> them, I am very interested.

The discussion was in this group, but your right, it has disappeared from
the server.
Look for it in Google / newgroups. I believe the discussion was called
"More memory"

Femme

Re:Unexpected heap overflow in protected mode program compiled by BP7


Dear Femme,

I have already found the article, thank you. And it did solve my
problem.

Gyula

--
Posted via http://dbforums.com

Re:Unexpected heap overflow in protected mode program compiled by BP7


I come late, but in:

Quote
> {$F+,O+} Program HeapTest; {$M 65520,0,655360}

> Const Max_X=1650;

> type
> { Ts=string[255];}
> TMatrix=array [0..Max_X] of pointer{^Ts};

> const ii=10000;
> var i,x:integer;M:array[1..ii] of ^TMatrix;

> Begin
> Writeln(MaxAvail,'  ',MemAvail);
> for i:=1 to ii do
> begin
> new(M);
> {for x:=0 to Max_X do New(M^[x]);}
> Writeln(MaxAvail,'  ',MemAvail);
> end;
> Readln;
> End.

an array of 10000 pointers is assigned memory 10000 times; that are
100.000.000 pointers and isn't that too much even for 64 MB ?

Georg.

Go to page: [1] [2]

Other Threads