[futurebasic] Re: [FB] STR# DUMMY again

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2001 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Wed, 01 Aug 2001 00:17:56 +0200
"Terrald J. Smith" wrote:
> 
> > If the program stops at that point it should indicate that you are
> > failing short on memory I think.
> > How did you dim the DIAGNOSI$ array?
> > Perhaps you could try to compact the memory before entering the loop
> > of death or allocate more RAM to your app and see what happens.
> 
> Actually, to test it, I increased the app RAM from 8MB to 23MB to see if
> that was an issue....no it still did it.  DIAGNOSIS$ array is dimmed as:  49
> DIM DIAGNOSIS$(18000).
> 
> I still think it is a problem with FN
> repElement(2,_filedataSTR,itemnumber$).  This code works fine in FBII but
> not now.
> 
> Terrald J. Smith, M.D.
> 

Terrald,

Could you try again with that modified piece of code:

LONG IF itemnumber$<>MODIFIED$      <--------------here is the problem
  Filesize& = MEM(_maxAvail)
  resHndl& = FN NEWHANDLECLEAR(2)
  LONG IF resHndl&
    Filesize&   = LOF(FILENUMBER,1)
    itemnumber% = Filesize&\\48
    StrHndl&    = FN NEWHANDLE(Filesize&)
    LONG IF StrHndl&
      CALL HLOCK(StrHndl&)
      READ FILE#FILENUMBER,[StrHndl&],Filesize&
      XREF@ 47 theStr$(_maxInt)
      theStr& = StrHndl&
      FOR MyLoop = 1 TO itemnumber%
        DIAGNOSIS$(Myloop) = theStr$(MyLoop-1)
        LONG IF FN PTRANDHAND(@DIAGNOSIS$(Myloop),resHndl&,48) = _noErr
          resHndl&..nil%++
        XELSE
          STOP "PTRANDHAND has failed!"+ STR$(MEM(_maxAvail))
          EXIT FOR
        END IF
      NEXT 
      CALL HUNLOCK(StrHndl&)
      CALL DISPOSEHANDLE(StrHndl&)
      FN repElement(2,_filedataSTR,itemnumber$)
      FN pGreplaceRes(resHndl&,_"STR#",_diagnosislistSTR,"DIAGNOSISLIST")
    END IF
  END IF  
  gSaveTks&=1
END IF 

If the inner loop fails and you still have plenty of memory the
problem may reside elsewhere in your program but only shows up here.

I would try to store the DIAGNOSIS$ array in a global handle if it
is not a too heavy change and see what happens. If somewhere in your
code you are overriding memory, you will have probably to deal with
another issue perhaps more severe.

Finally, I would change the FN repElement call with a function that
I have recently adapted from an example by Apple which I believe is
doing quite the same thing (caution repElement has been used by
hundred of programmers as opposed to the following one that I have
never tested in real conditions nor released):

/*
     Usage:
          err% = FN SetIndString(theString$,ResID%,stringIndex%)

     This function sets the stringIndex-th string in the STR#
resource specified
     by ResID% and returns an error code (_resNotFound) if the
operation fails.
*/
CLEAR LOCAL MODE
'~'8
LOCAL FN SetIndString( @inStrP AS PTR, resID AS SHORT, item AS SHORT )
'~'9
  DIM AS UNSIGNED LONG offset'resource offset to str to replace
  DIM AS HANDLE resH'handle pointing to STR# resource
  DIM AS PTR    strP'string pointer to STR# string to replace
  DIM AS LONG   oldSize,newSize'size of STR# resource before and
after call
  DIM AS SHORT  i, err'counter to index up to strIndex

  err = _resNotFound
  IF resID < 1 THEN EXIT FN 
  resH = FN Get1Resource(_"STR#",resID)
  IF FN ResError != _noErr OR resH = _nil THEN EXIT FN'make sure
resource exists
  IF item < 1 OR item > resH..nil% THEN EXIT FN'make sure index is
in bounds

  HLock(resH)
  HNoPurge(resH)
  offset = SIZEOF(SHORT)
  strP   = [resH] + SIZEOF(SHORT)'get a pointer to the string to replace
  FOR i = 1 TO item - 1
    offset += 1 + strP.nil`
    strP   += 1 + strP.nil`
  NEXT
  oldSize = FN GetHandleSize(resH)'grow/shrink resource handle to
make room for new string
  newSize = oldSize - strP.nil` + inStrP.nil`
  HUnlock(resH)
  SetHandleSize(resH,newSize)
  LONG IF FN MemError
    ReleaseResource(resH)
    EXIT FN
  END IF
  HLock(resH)
  strP = [resH] + offset
/* move old data forward/backward to make room */
  BlockMove(strP + strP.nil` + 1, strP + inStrP.nil` + 1, oldSize -
offset - strP.nil` - 1)
  BlockMove(inStrP,strP,inStrP.nil` + 1)'move new data in 
  ChangedResource(resH)'write resource out
  WriteResource(resH)
  HPurge(resH)
  ReleaseResource(resH)
  err = FN ResError
END FN = err
-- 

Cheers

Alain

-----------------------------------------------------
FB^3 in Europe:  http://euro.futurebasic.com/
FB II Pouch:     http://www.pixmix.com/FB/outils.html
-----------------------------------------------------