Memory Allocate Crash?
Posted: Fri Jul 16, 2010 4:18 pm
				
				Ok.  I've been hammering on this for a couple of weeks now.  This is a subsection of one of my programs. What its supposed to do is escape Hyphens (referred to as ticks in the program) and turn a single into a double tick.  All this is performed in memory.  I am unable to get much consistency out of the error, other than, if I select about 10 or 20 files, it will crash on one of the pointers, the memory allocation, or debug output on a memory pointer.  You can put any set of files through it.  It should just read the bytes and expand single ticks (')  to double ticks ('') in memory and exit. 
I would appreciate if anyone having some spare time would take a look and see if they are able to duplicate the error.
TIA.
Here it is.
			I would appreciate if anyone having some spare time would take a look and see if they are able to duplicate the error.
TIA.
Here it is.
Code: Select all
Procedure.i CountTicks(*StringBuffer)                                           ;; count the ticks
  tickCount = 0                                                                 ;; init tick count
  fileLength = MemorySize(*StringBuffer)                                        ;; get the file length
  For c = 1 To fileLength                                                       ;; iterate through the file
    *thisChar = *StringBuffer + c                                               ;; look at each character
    If PeekB(*thisChar) = 39                                                    ;; if its a tickmark
      tickCount = tickCount + 1                                                 ;; count it
    EndIf                                                                       ;; end if its a tickmark
  Next                                                                          ;; next character
  ProcedureReturn (tickCount)                                                   ;; done retun the count
EndProcedure
;----------------------------------------------
; open a directory window
; select file or types
;----------------------------------------------
Title$ = "Select files to import."                                            ;; Title for window
DefaultFile$ = ""                                                             ;; No default file
;Pattern$ =  "All Files (*.sql,*.txt)|*.sql;*.txt|" +                          ;; the pattern to pass the filter box
;_  "SQL Files (*.sql)|*.sql|Text Files (*.txt)|*.txt"                         ;; the pattern to pass the filter box
;Pattern.i = 0                                                                 ;; the pattern selection is none
gFilename$ = OpenFileRequester(                                               ;; open
_           Title$, DefaultFile$, Pattern$, Pattern,                          ;; dialog
_           #PB_Requester_MultiSelection)                                     ;; box so we can request a file(s)
While gFilename$                                                              ;; if there is a gFilename$
  
  
  
  
  If ReadFile(0, gFilename$)                                                  ;; can we read it if so
    
    
    ;----------------------------------------------
    ; make sure a non zero length file exists
    ;----------------------------------------------
    gFileLength = Lof(0)                                                      ;; read length of file
    If gFileLength > 0                                                        ;; if length is zero run 'else' portion
      
      ;----------------------------------------------
      ; allocate memory
      ;----------------------------------------------
      ;*gFile = ReAllocateMemory(*gFile, gFileLength)                          ;; set/reset memory buffer
      *gFile = AllocateMemory(gFileLength)                                    ;; set/reset memory buffer
      
      
      checkLength = MemorySize(*gFile)                                        ;; get the length of memory buffer
      
      ;----------------------------------------------
      ; verify allocated memory length correct
      ;----------------------------------------------
      If checkLength = gFileLength                                            ;; verify memory allocated
        
        ;----------------------------------------------
        ; read file into memory
        ;----------------------------------------------
        checkLength = 0                                                       ;; reset
        checkLength = ReadData(0, *gFile, gFileLength)                        ;; put file in memory
        
        ;----------------------------------------------
        ; verify file in memory is correct length
        ;----------------------------------------------
        If checkLength = gFileLength                                          ;; verify read properly
          
          
          ;----------------------------------------------
          ; count amount to add to file
          ;----------------------------------------------
          lengthToAdd = CountTicks(*gFile)                                    ;; length to add to buffer
          
          ;----------------------------------------------
          ; create a new length variable
          ;----------------------------------------------
          newLength = gFileLength + lengthToAdd                               ;; calculate new file length
          
          ;----------------------------------------------
          ; set up the new
          ; length of the main file
          ;----------------------------------------------
          Delay(200)
          *gFile = ReAllocateMemory(*gFile, newLength)                        ;; change length of *buffer
          Delay(200)
          
          ;----------------------------------------------
          ; finally set up pointers
          ;----------------------------------------------
          *leftPointer = *gFile + gFileLength                                 ;; points to the end of original file
          *rightPointer = *gFile + newLength                                  ;; points to the end of the entire file
          Delay(200)
          
          Debug(Str(MemorySize(*gFile)) + " " + Str(*leftPointer) + " " + Str(*rightPointer))
          
          
          ; --------------------------------------------
          ;  if using assembler the
          ;  available registers are: eax, edx and ecx
          ; --------------------------------------------
          
          ticksToGo = lengthToAdd
          
          
          ;DEC *leftPointer                                       ;; start on the correct character
          *leftPointer = *leftPointer - 1
          
          While ticksToGo > 0                                     ;; have more to do
            ;Debug(*leftPointer)
            
            leftByte = PeekB(*leftPointer)
            rightByte = PeekB(*rightPointer)
            
            If leftByte = 39                                      ;; if its a tick mark
              *rightPointer = *rightPointer - 1                   ;; move right side pointer left
              PokeB(*rightPointer,leftByte)                       ;; write the tick mark
              *rightPointer = *rightPointer - 1                   ;; move right side pointer left
              PokeB(*rightPointer,leftByte)                       ;; write the tick mark again
              ticksToGo = ticksToGo - 1                           ;; loop control
            Else                                                  ;; otherwise
              *rightPointer = *rightPointer - 1                   ;; move right side pointer left
              PokeB(*rightPointer,leftByte)                       ;; write the byte
            EndIf                                                 ;; end if its a tick
            
            *leftPointer = *leftPointer - 1                       ;; get ready to get next character
            
          Wend
          
          *leftPointer = 0
          *rightPointer = 0
          
        EndIf                                                                       ;;
        
        
        ;;;;;;;;;
        
      Else
        MessageRequester ("File Load Error", "Not able to read file into memory: " + gFilename$,#PB_MessageRequester_Ok)
      EndIf                                                       ;; endif read file
      
    Else
      MessageRequester ("Insufficient Memory", "Insufficient memory for file: " + gFilename$,#PB_MessageRequester_Ok)
    EndIf                                                         ;; check memory was allocated
    
  Else
    ;; write the file name only on the card
    
  EndIf                                                           ;; if gFileLength > 0
  
  
  Debug(gFilename$ + " " + Str(MemorySize(*gFile)))
  
  FreeMemory(*gFile)
  gFilename$ = NextSelectedFileName()                             ;; goto next select file
  
Wend                                                            ;; end if gFilename$