mint 18 x64 - pb 5.43 - PeekB [Error] Invalid memory access

Linux specific forum
jjStinger
New User
New User
Posts: 3
Joined: Thu Oct 20, 2016 12:50 pm

mint 18 x64 - pb 5.43 - PeekB [Error] Invalid memory access

Post by jjStinger »

I'm running into a bit of a problem with some code that's been "in production" for quite some time. I'm getting [ERROR] - Invalid memory access on the PeekB statement. I've included a a functional test below (works in windows 7 32 bit, memory access error on linux 64) . Any thoughts or theories on what i may be doing wrong, or missing? Compiling /building on linux is "new" and just something i'm trying with this code. All of the production stuff builds and runs swimmingly on my 32 bit windows VMs. Any insight in to what im missing would be most appreciated :)

Couple of random facts...
  • the code functions as expected in a windows 32 bit environment, building with pb 5.43
  • according to checkinstall.sh 'Everything seems correctly setup for PureBasic !'
  • Create Unicode executable is off in compiler options
couple of things i tried...
  • Rolled back PB to 5.31, and 5.22 on the 64 bit Linux... results are the same (invalid memory access)
  • Ran pb as root... results are the same (invalid memory access)
  • all combinations of the debugger and purifier on/and off... results are the same (invalid memory access)
  • various combinations of forcing the procedures to #PB_Ascii where available... results are the same (invalid memory access)
... the fact that prior versions of pb have the same result on the linux box indicate to me that this is a system or config issue, but im all ears (??)

a functional (as in it will error on the linux side, and not windows 7 32 bit )snippit and test bed of the code...

Code: Select all

;Linux Mint 18 - Cinnamon 3.0.7 
;
;uname -a
;Linux J-LINUX 4.4.0-43-generic #63-Ubuntu SMP Wed Oct 12 13:48:03 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
;
;lshw -short
; H/W path               Device      Class          Description
; =============================================================
;                                    system         K30AD_M31AD_M51AD (All)
; /0                                 bus            K30AD_M31AD_M51AD_M32AD
; /0/0                               memory         64KiB BIOS
; /0/1                               memory         
; /0/1/0                             memory         8GiB DIMM DDR3 Synchronous 1600 MHz (0.6 ns)
; /0/39                              memory         System Memory
; /0/39/0                            memory         DIMM [empty]
; /0/3d                              memory         256KiB L1 cache
; /0/3e                              memory         1MiB L2 cache
; /0/3f                              memory         6MiB L3 cache
; /0/43                              processor      Intel(R) Core(TM) i5-4440 CPU @ 3.10GHz
; /0/2                               memory         
; /0/3                               memory         
; /0/100                             bridge         4th Gen Core Processor DRAM Controller
; /0/100/1                           bridge         Xeon E3-1200 v3/4th Gen Core Processor PCI Express x16 Controller
; /0/100/1/0                         display        GK104 [GeForce GTX 760 OEM]
; /0/4/0.0.0             /dev/sda    disk           1TB TOSHIBA DT01ACA1

EnableExplicit

Global.l    gInputBufferStart, gInputBufferEnd
Global.l    gInputBufferSave
Global.l    gInputBufferSize

Global      *MemoryID_Input

Global.s    delimiterSegment

Global.s    rawFileData

Global.l    currentSegmentStart
Global.l    segmentState

Enumeration
   #SEG_STATE_NONE      = 0
   #SEG_STATE_NEXT      = 1
   #SEG_STATE_PREVIOUS  = 2
   #SEG_STATE_FIRST     = 3
   #SEG_STATE_LAST      = 4
EndEnumeration

Procedure.S nextSegment()
   
   Define.L DelimiterPos
   Define.b AscSegSep
   Define.S  Segment
   
   DelimiterPos.L = gInputBufferStart
   Debug gInputBufferStart
   AscSegSep = Asc(delimiterSegment)
   
   Repeat
      
      ;- HERE - [ERROR] Invalid memory access.
      If PeekB(DelimiterPos) = AscSegSep
                  
         ;MessageRequester ("In There?","yep")
         
         ; found end of segment
         currentSegmentStart = gInputBufferStart
         Segment.S = PeekS(gInputBufferStart, DelimiterPos - gInputBufferStart)
         
         ; increment segment pointer
         gInputBufferStart = DelimiterPos + 1
         
         ; done - return next segment
         Break
         
      Else
         
         ; next character
         DelimiterPos + 1
         
         ; done if past end of input file buffer
         If DelimiterPos > gInputBufferEnd
            Break
         EndIf
                  
      EndIf
      
   ForEver
   
   segmentState = #SEG_STATE_NEXT
   
   ; return next segment
   ProcedureReturn Segment
   
EndProcedure

Procedure.S firstSegment()
   
   ;reset buffer pointer 
   gInputBufferStart= gInputBufferSave
   segmentState = #SEG_STATE_FIRST
   
   ProcedureReturn NextSegment()
   
EndProcedure

Procedure.l loadTheFile(fileToRead.s)
   
   Define.l fH
   
   Define.l bytes, length
   Define.L hasReturnFile, originalLen, hasTrailingSpaces
   Define.l CR_isDelimiter, LF_isDelimiter
   
   fH = ReadFile(#PB_Any , fileToRead);,#PB_Ascii) 
   If fH
      
      ; allocate memory buffer
      ; entire file
      length = Lof(fH)
      
      *MemoryID_Input = AllocateMemory(length)            
      
      If *MemoryID_Input
         
         ; read file
         bytes.L = ReadData(fH, *MemoryID_Input, length)
         
         
      ; convert data to string
         
         CompilerIf #PB_Compiler_Version >= 540
            rawfileData = PeekS(*MemoryID_Input, length,#PB_Ascii )
         CompilerElse
            rawfileData = PeekS(*MemoryID_Input, length)
         CompilerEndIf
            
      FreeMemory(*MemoryID_Input)
      
      gInputBufferStart = @rawFileData
      gInputBufferSave = @rawFileData
      gInputBufferEnd = @rawFileData + length - 1
         
      EndIf
      
      CloseFile(fh)
      
   EndIf
   
   ProcedureReturn fh
   
EndProcedure

;- === TESTBED EXECUTION STARTS HERE

Define.s    theFileName

;theFileName ="/mnt/data/work/1200100000001938FF-GS101461227-JJ-REQ.TXT"

;Pick a text file, any text file
theFileName = OpenFileRequester("Select a file","","Text (.txt)|*.txt|All files (*.*)|*.*",0)

If loadTheFile(theFileName)
   
   delimiterSegment= "~"
   
   Debug firstSegment()
   Debug nextSegment()
   Debug nextSegment()
   
   CallDebugger   
   
EndIf
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: mint 18 x64 - pb 5.43 - PeekB [Error] Invalid memory acc

Post by Kukulkan »

I have no idea, but each time I've had such problems, it was always a PeekS() issue. The main point is, that the length parameter of PeekS() expects the number of characters(!) and not bytes! I fixed many issues by allocating two bytes more and poking zero to it (PokeW() or check PokeC()). After that, I used PeekS() with a length of -1 to let it stop at the 0 byte terminators.

Maybe this solves your issues?
jjStinger
New User
New User
Posts: 3
Joined: Thu Oct 20, 2016 12:50 pm

Re: mint 18 x64 - pb 5.43 - PeekB [Error] Invalid memory acc

Post by jjStinger »

Great tip! and put me on to the solution.

... all of my buffer addressed were defined as longs... making them Quads stored the actual memory addresses i should have been peeking. It's not the first time i've gotten burned by getting stingy with my data types / sizes. Thanks for the help, and um... nothing to see here ;)
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: mint 18 x64 - pb 5.43 - PeekB [Error] Invalid memory acc

Post by Kukulkan »

Hi, great that I was able put you on the right lane :-)

I think you should use pointers or integer instead of quads:

Code: Select all

Global.i gInputBufferStart, gInputBufferEnd
; or even better
Global *gInputBufferSave
Best,

Kukulkan
jjStinger
New User
New User
Posts: 3
Joined: Thu Oct 20, 2016 12:50 pm

Re: mint 18 x64 - pb 5.43 - PeekB [Error] Invalid memory acc

Post by jjStinger »

a quick stroll through the help file pointed me at .i integers as well, a pointer makes even more sense as it becomes "self documenting"... appreciate the help, and cheers!
Post Reply