Announcement

Collapse
No announcement yet.

Redim Preserve / Memory Problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Michael Mattias
    replied
    How do you code around a ReDim Preserve?
    Well, first I try to figure out if I can size the array once. FOr example, I read the input data file twice: once just to count entries, the second time to load the data.

    An approach I used in one application was to REDIM only in "blocks" of many elements, as in..

    Code:
      %Array_Init_Size = 1000
      %Array_Incr      = 100
    
       REDIM Foo(%Array_Init_Size)   ' initial setting
    
    AddItems:
        DO 
            yadda, yadda
            IF ItemCount > UBOUND (Foo,1) THEN
              REDIM PRESERVE Foo (UBOUND(Foo,1) + %Array_incr) ' add %array_incr elements to the array
            END IF
            yadda, yadda
        LOOP UNTIL ISTRUE(ArrayLoaded)
    There is one more approach, but I haven't used that "for real" since PB/DOS days, and that's I started done that yet, and that's to dynamically allocate memory and keep it in a linked list.

    Oops, forgot one: we can wait for Microsoft to fix Win98.

    MCM


    Leave a comment:


  • Lance Edmonds
    replied
    Code:
    ON ERROR RESUME NEXT
    REDIM PRESERVE myarray(params)
    IF ERR = 7 THEN MSGBOX "uh oh... Low or fragmented memory!"


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Tim Wisseman
    Guest replied
    Interesting. . .
    How do you code around a ReDim Preserve?

    Tim


    ------------------

    Leave a comment:


  • Michael Mattias
    replied
    Whoa, there, not so fast!

    There IS a problem with this, but it is a problem with Win98, not PB.

    I had the same problem; when I sent it in to PB Support, they tested on Win98 and NT, and there was no problem on NT.

    You will find if you trap errors that one of your REDIMs will return an "Error 7 (Insufficient Memory) when fragmentation causes REDIM PRESERVE to fail.

    So...

    What I do now is code around a lot of REDIM PRESERVE statements.



    ------------------
    Michael Mattias
    Racine WI USA
    [email protected]

    Leave a comment:


  • Tim Wisseman
    Guest replied

    Never mind. . .

    An out of bounds array in another part of the DLL was
    causing massive memory corruption that lead to the
    ReDim failure.

    The DLL was not running out of memory resources. . .

    Tim


    ------------------

    Leave a comment:


  • Tim Wisseman
    Guest started a topic Redim Preserve / Memory Problem

    Redim Preserve / Memory Problem

    The following is code from a DLL that is causing a Unhandled
    Exception 0x00005 in Kernel32.dll. After this my system is very
    unstable until I reboot. I can not even open a folder without
    explorer crashing with an exception in User32.dll. . .

    So I am guessing it is some sort of memory problem. As in my
    program is using up some memory resource.

    GLOBAL m_mark() AS INTEGER
    . . .
    Redim m_mark(1 to 100)
    . . .

    Dim v As Long

    v = UBound(m_mark) + 1000
    ReDim Preserve m_mark(1 To v)

    I know that the value of UBound(m_mark) is 100 when this happens.
    This is part of the program that has not been touched in years
    and has never had a problem before.

    My system has 53% USER Free memory and 76% GDI free after the
    crash event.

    My questions are:

    When I do a ReDim Preserve were does the memory it uses come from?
    Stack? Heap? DGroup?

    Is there an easy why to get more of what ReDim Preserve needs?

    Any ideas on how to work around this barrier?

    Tim


    ------------------
Working...
X