Page 1 of 1

Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 11:47 am
by MrCor
Hi,

Frequently I get an error: Invalid memory access: (write error at address 0). Sometimes the number is higher then 0. Sometimes I get a read-error instead of a write-error.
As an example below code whereas this error appears. It is a WIP (Work In Progress).
The code is pretty simple and straightforward, so no extraordinary things there I think. The section the error appears contains a high variable: 11183522 and this variable is used to define an (also large) array(). When there are no errors, the program runs fine, and I try to approach the array, I get the same error too.
The error in this example appears in the section where it says ";--- locaties in array Locaties$()". It is persistent and comes up every time I run it.
I removed all the overhead (such as opening a window and gadgets). That does not do the trick.
However, if I use only the code up to that section (the rest is not there), this runs normal without the error.
I also write codes whereas this sort of error sometimes appears and next time I run it does not.

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

Code: Select all

path$ = "G:\_Cor\PureBasic\Hackers\"

;- scherm openen
OpenWindow(0, 0, 0, 300, 500, "Hackers", #PB_Window_ScreenCentered)
;--- gadgets
TextGadget(0,  10, 10, 150, 20, "")
TextGadget(1,  10, 30, 150, 20, "")
TextGadget(2,  10, 50, 150, 20, "")
TextGadget(3,  10, 70, 150, 20, "")

;- data inlezen
;--- criteria hacker in array CriteriaHackers$()
ReadFile(0, Path$ + "CriteriaHackers.txt")
SetGadgetText(0, "Inlezen criteria hackers")
Dim CriteriaHackers$(0)
AantalCriteria  = 0
While Not Eof(0)
  regel$                            = ReadString(0)
  AantalCriteria                 = AantalCriteria + 1
  ReDim CriteriaHackers$(AantalCriteria)
  CriteriaHackers$(AantalCriteria)  = regel$
Wend
CloseFile(0)

;--- locaties in array Locaties$()
SetGadgetText(1, "Inlezen locaties")
AantalLocaties = 11183522
ReadFile(0, path$ + "Geografie.txt")
Dim Locaties$(AantalLocaties, 2)
For a = 1 To AantalLocaties
  regel$          = ReadString(0)
  pos1            = FindString(regel$, "#")
  pos2            = FindString(regel$, "#", pos1 + 1)
  Locaties$(a, 0) = Left(regel$, pos1 - 1)
  Locaties$(a, 1) = Mid(regel$, pos1 + 1, pos2 - pos1 - 1)
  Locaties$(a, 2) = Right(regel$, Len(regel$) - pos2)
Next a
CloseFile(0)

;--- hackers uit webserv.log filteren en in array Hackers$()
;----- eerst alle IPadressen uit webserv.log halen
SetGadgetText(3, "IPadressen verzamelen")
ReadFile(0, Path$ + "webserv.log")
tmp  = 0
Dim tmp$(0)
While Not Eof(0)
  regel$            = ReadString(0)
  pos               = FindString(regel$, " ")
  IPadres$          = Left(regel$, pos - 1)
  hit               = 0
  For a = 1 To tmp ; checken of IPadres al voorkomt
    If IPadres$ = tmp$(a)
      hit = 1
      Break
    EndIf
  Next a 
  If hit = 0
    tmp = tmp + 1
    ReDim tmp$(AantalIPadressen)
    tmp$(tmp) = IPadres$
  EndIf
Wend
CloseFile(0)
;----- IPadressen$ filteren mbv Bots en zo.txt. Hier zit ook ons eigen IPadres in, zowel intern als extern
ReadFile(0, path$ + "Bots en zo.txt")
AantalBots        = 0
AantalIPadressen  = 0
Dim Bots$(0)
While Not Eof(0)
  AantalBots = AantalBots + 1
  ReDim Bots$(AantalBots)
  Bots$(AantalBots) = ReadString(0)
Wend
Dim IPadressen$(0)
For a = 1 To AantalIPadressen
  regel$  = tmp$(a)
  hit     = 0
  For b = 1 To AantalBots
    If regel$ = Bots$(b)
      hit = 1
      Break
    EndIf
  Next b
  If hit = 0
    AantalIPadressen = AantalIPadressen + 1
    ReDim IPadressen$(AantalIPadressen)
    IPadressen$(AantalIPadressen) = regel$
  EndIf
Next a
    



Repeat
  WaitWindowEvent()




ForEver
--------------------------------------------------------------------------------------------------

Suggestions on writing this code in a different way are welcome of course, but it would be nice to see a (general?) cause and hopefully a solution (for this particulair code).

An idea....
Windows contains bugs and so does the memorymanager. Could these errors be caused by the memorymanager? In this case there would be nothing I could do about it of course. I use Windows 10, home 64-bits. BTW, the mentioned errors also appeared in previous Windows versions.

Thanks in advance for your feedback. Questions about it? Just ask.

Cheers, Cor

// Code-Tags added (Kiffi)

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 12:11 pm
by Caronte3D
Do you think you have enough RAM to create a monster array of that size? :shock:

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 12:18 pm
by miskox
Caronte3D wrote: Fri Oct 14, 2022 12:11 pm Do you think you have enough RAM to create a monster array of that size? :shock:
Another option: do you run this on x86? 4GB memory limit might be a reason (thanks Caronte3D for a hint).

Saso

(I had 32-bit Windows installed, 8 GB RAM but only 4GB was available (seen by the OS) for Windows. And now you know where the problem was - 32-bit Windows instead of 64-bit).

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 12:20 pm
by Caronte3D
Also, I think you for loop must begin whith 0 ?

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 1:33 pm
by MrCor
16GB RAM
No significant other programs running
Already compiling x86 to make programs run on 32 bits systems
Why would a loop has to start with 0? Just asking.

Update...
Changed the variable to AantalLocaties.q = 11183522 adding the .q
It works fine now. It looks as if some part of that variable (bytes) is ignored furtheron in the program and it causes a fault. Does not anymore after about 20 testruns.
Probably more times this happened in other programs.

Thanks for the help everbody.

Cheers, Cor

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 1:50 pm
by NicTheQuick
MrCor wrote: Fri Oct 14, 2022 1:33 pmAlready compiling x86 to make programs run on 32 bits systems
Why are you doing this? Nearly nobody uses x86 today. Use 64 bit and you will not have any issues with memory again.
MrCor wrote: Fri Oct 14, 2022 1:33 pmWhy would a loop has to start with 0? Just asking.
Because the first element in an array has index 0 and not 1.

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 1:53 pm
by MrCor
I have several systems at home that still run on 32 bits. Seems like a good reason to me. I do not program for other people, too much hassle.
Of course an array starts with 0 but why would I start with that? Starting from 1 works fine and should also work fine. Hardly any reason why a program would give faults....

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 2:17 pm
by Kiffi
Topic locked because of questionable code.

Edit: Unlocked again because MrCor could plausibly explain to me what he needed the code for.

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 9:16 pm
by TassyJim
The usual cause for my memory problems when switching between 32 and 64 bit is using a long where it should be an integer.

Re: Invalid memory access: (write error at address 0)

Posted: Fri Oct 14, 2022 11:15 pm
by BarryG
Kiffi wrote: Fri Oct 14, 2022 2:17 pmquestionable code
That's what I thought, because it's from a new user and mentions hacking, bots, and IP addresses.