Invalid memory access: (write error at address 0)

Just starting out? Need help? Post your questions and find answers here.
MrCor
User
User
Posts: 22
Joined: Tue Dec 01, 2015 8:31 pm

Invalid memory access: (write error at address 0)

Post 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)
User avatar
Caronte3D
Addict
Addict
Posts: 1362
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

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

Post by Caronte3D »

Do you think you have enough RAM to create a monster array of that size? :shock:
miskox
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

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

Post 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).
User avatar
Caronte3D
Addict
Addict
Posts: 1362
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

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

Post by Caronte3D »

Also, I think you for loop must begin whith 0 ?
MrCor
User
User
Posts: 22
Joined: Tue Dec 01, 2015 8:31 pm

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

Post 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
User avatar
NicTheQuick
Addict
Addict
Posts: 1523
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
MrCor
User
User
Posts: 22
Joined: Tue Dec 01, 2015 8:31 pm

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

Post 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....
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

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

Post by Kiffi »

Topic locked because of questionable code.

Edit: Unlocked again because MrCor could plausibly explain to me what he needed the code for.
Hygge
TassyJim
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

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

Post 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.
BarryG
Addict
Addict
Posts: 4178
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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.
Post Reply