Board index » delphi » Problems with a strange heap overflow

Problems with a strange heap overflow

  Hello people.

I'm writing a text-window interface, so I must do a large use of the
heap through Get and FreeMem procedures.
Working on this program, it often breaks up with a heap overflow
error. This is strange, 'cause I sistematically get and free the mem
when it's time to do it. So, I've tried to find a solution, and
checked the memavail and maxavail values printing them on the screen
during the runtime. I've noticed that the freemem procedure doesn't
free the memory immediately, but only when the remaining space in the
heap is near the zero (I mean about 5000-2000 bytes free). But
sometimes, instead of freeing the heap, it gives me this heap overflow
error.
Thank you in advance for your help. I would like to know if there is
something wrong in the structure, and if you would like to see the
source only ask me to.

  Nice greetings from Italy, Marcello :-)

      /~~\                              MARCELLO RAFFA -- ITALY
   +--|__|----------)  /-----------------------------------------\
  () -____-   [__]  |  | E-Mail: sra...@eniware.it               |
 _+---              |[]| News: it.comp.*, TPascal and UFO groups |[
/__;-(__)-----(__)--/  \---(__)-------(__)-------(__)-------(__)-/

 

Re:Problems with a strange heap overflow


Quote
sra...@eniware.it (Marcello Raffa) wrote:
>  Hello people.
>I'm writing a text-window interface, so I must do a large use of the
>heap through Get and FreeMem procedures.
>Working on this program, it often breaks up with a heap overflow
>error. This is strange, 'cause I sistematically get and free the mem
>when it's time to do it. So, I've tried to find a solution, and
>checked the memavail and maxavail values printing them on the screen
>during the runtime. I've noticed that the freemem procedure doesn't
>free the memory immediately, but only when the remaining space in the
>heap is near the zero (I mean about 5000-2000 bytes free). But
>sometimes, instead of freeing the heap, it gives me this heap overflow
>error.
>Thank you in advance for your help. I would like to know if there is
>something wrong in the structure, and if you would like to see the
>source only ask me to.
>  Nice greetings from Italy, Marcello :-)

Hi Marcello,
"I've noticed that the freemem procedure doesn't free the memory
immediately" - it couldn't be... FreeMem is just a procedure to free a
part of heap, so this memory WILL be released IMMEDIATELY. I'm not
sure about memavail and maxavail, I should check RTL about that. I'll
tell you tomorrow how it's done... So - check your code.

Cheers
  Dmitri

--------------------------------------------------------------
        Shit happens, but why it always happens to us?
--------------------------------------------------------------
Dmitri Poujlivyi            mailto:  dmi...@god.bel.alcatel.be
                            http://www.dma.be/p/bewoner/dmitri

Re:Problems with a strange heap overflow


Quote
sra...@eniware.it (Marcello Raffa) wrote:
>during the runtime. I've noticed that the freemem procedure doesn't
>free the memory immediately, but only when the remaining space in the
>heap is near the zero (I mean about 5000-2000 bytes free). But

Although freemem releases the memory at once it may not show up on MaxAvail
immediately because that shows the largest single block of memory available.
Unless you release memory in exactly the reverse sequence that you
allocated it your heap may become "fragmented" (ie. full of holes) so that
you have a mixture of blocks of available and allocated memory and MaxAvail
will only show the largest of the available blocks. Memavail on the other
hand will show the total of all free blocks but of course that does not
mean that you canm allocate that much memory in one go.

It is not always possible to release memory in exactly the reverse order of
allocation but if you can do that it does make debugging and finding
un-released blocks much easier.

Mark.
--

   * Meeeow ! Call Spuddy on (01268) 515441 for FREE mail & Usenet access *

Other Threads