WriteData and Invalid memory access

Just starting out? Need help? Post your questions and find answers here.
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

WriteData and Invalid memory access

Post by Simo_na »

Hi, this code (64 bit compiler - Windows 10 64 bit) get error, i have 32 Gb of DDR4 RAM on Mobo.
where am I wrong ?

Code: Select all

Global.q gigs = (4*1000)*1024*1024       ; 4 Gigabyte
*MemoryID = AllocateMemory(gigs)         ; allocating a 4 Gigabyte memory block
FillMemory(*MemoryID,gigs,255,#PB_Byte)  ; fill a 4 Gigabyte memory block with a 255 byte value
CreateFile(0,"Text.txt")                 ; create a new file
FileBuffersSize(0,131072)                ; create a new buffer of 128K
WriteData(0, *MemoryID, gigs)            ; write the 4 Gigabyte memory block into the file
FlushFileBuffers(0)                      ; flush buffer
CloseFile(0)                             ; close the opened file
LOG
[12:39:55] Waiting for executable to start...
[12:39:55] Executable type: Windows - x64 (64bit, Unicode)
[12:39:55] Executable started.
[12:39:56] [ERROR] test4gb.pb (Line: 6)
[12:39:56] [ERROR] Invalid memory access. (write error at address 36110336)
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

Re: WriteData and Invalid memory access

Post by ricardo_sdl »

You always get the same error? Or does it work sometimes?
You can check my games at:
https://ricardo-sdl.itch.io/
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: WriteData and Invalid memory access

Post by Simo_na »

ricardo_sdl wrote: Tue Feb 15, 2022 3:15 pm You always get the same error? Or does it work sometimes?
Always, same error.

And on the disk drive i have 202 GB free of 465 GB.
User avatar
Caronte3D
Addict
Addict
Posts: 1029
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: WriteData and Invalid memory access

Post by Caronte3D »

Despite the error...
I think 4GB = 1024*1024*1024*4 (4294967296) :?

Also... you should check the "CreateFile" return to know if the file is created ok
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: WriteData and Invalid memory access

Post by Simo_na »

Caronte3D wrote: Tue Feb 15, 2022 4:42 pm I think 4GB = 1024*1024*1024*4 (4294967296) :?
gigs = (2*1023)*1024*1024 = 2145386496
here no error, all done

gigs = (2*1024)*1024*1024 = 2147483648
here the error jump out

as you pass the 2145386496...the error comes out :x
User avatar
Caronte3D
Addict
Addict
Posts: 1029
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: WriteData and Invalid memory access

Post by Caronte3D »

So... a limit of 2GB is somewhere :?
Wait until more expert users take a look at it.
normeus
Enthusiast
Enthusiast
Posts: 414
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: WriteData and Invalid memory access

Post by normeus »

Such a small disk is probably FAT32 with a file size limit of 4GB. Try to write the file in a couple of writes.

Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

Re: WriteData and Invalid memory access

Post by ricardo_sdl »

Same error here (win 10-64 bit, 8 GB of ram). As a test I wrote an equivalent program in php:

Code: Select all

php -d memory_limit=-1 -r "$a=str_repeat('a', 4*1024*1024*1024); $f=fopen('./text.txt', 'wb'); fwrite($f, $a, 4*1024*1024*1024);"
And it reported an error: "Notice: fwrite(): Write of 4294967295 bytes failed with errno=0 No error in Command line code on line 1", but it actually wrote the 4.294.967.295 bytes in the file.
You can check my games at:
https://ricardo-sdl.itch.io/
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: WriteData and Invalid memory access

Post by Simo_na »

Caronte3D wrote: Tue Feb 15, 2022 7:12 pm So... a limit of 2GB is somewhere :?
Wait until more expert users take a look at it.
Thank you
normeus wrote: Tue Feb 15, 2022 7:16 pm Such a small disk is probably FAT32
File system here is NTFS.
I will make files of 1 gb each, not a bad idea.

Thank you
ricardo_sdl wrote: Tue Feb 15, 2022 7:27 pm Same error here (win 10-64 bit, 8 GB of ram).
:?
infratec
Always Here
Always Here
Posts: 6818
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: WriteData and Invalid memory access

Post by infratec »

Simo_na wrote: Tue Feb 15, 2022 6:29 pm gigs = (2*1024)*1024*1024 = 2147483648
here the error jump out
This value correponds to:

Code: Select all

Integer 	.i 	4 bytes (using 32-bit compiler) 	-2147483648 to +2147483647
The help for WriteDate() looks like:

Code: Select all

Result = WriteData(#File, *MemoryBuffer, Length)
Values without an explicit type are ... .i

You are using PB x86 or PB x64 :?:
With PB x86 the 'error' is 'normal'
With PB x64 it should work.

Btw. we have to ask Fred f the length parameter is .q or .i
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: WriteData and Invalid memory access

Post by netmaestro »

infratec: OP says 64.
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: WriteData and Invalid memory access

Post by netmaestro »

Probably a limitation of WriteData(). This works:

Code: Select all

Global gigs.q = 4*1024*1024*1024   ; 4 Gigabyte

*MemoryID = AllocateMemory(gigs)        
If *MemoryID > 0
  FillMemory(*MemoryID,gigs,255,#PB_Byte)  
  If CreateFile(0,GetTemporaryDirectory()+"\Text.txt")                
    FileBuffersSize(0,#MAXWORD)                                        
    *writeptr=*MemoryID
    *end = *MemoryID + gigs
    While *writeptr < *end
      If *end-*writeptr >= #MAXWORD
        chunksize = #MAXWORD
      Else 
        chunksize = *end-*writeptr
      EndIf
      WriteData(0, *writeptr, chunksize)            
      *writeptr + chunksize
    Wend
    FlushFileBuffers(0)                     
    CloseFile(0)                             
  EndIf
EndIf
BERESHEIT
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: WriteData and Invalid memory access

Post by Simo_na »

infratec wrote: Tue Feb 15, 2022 8:14 pm

Code: Select all

Integer 	.i 	4 bytes (using 32-bit compiler) 	-2147483648 to +2147483647
Confirm
OP says 64 :)
Thank you
netmaestro wrote: Tue Feb 15, 2022 9:38 pm Probably a limitation of WriteData().
ReadData have same things ?




What runs in your veins ? is HEX ? :shock:

xor cx,cx
nop
Thank You
cmp cx, FF
jle
:)
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: WriteData and Invalid memory access

Post by Simo_na »

Ok, I explain the code:
I write the 4 Giga with the value 'nine'
then I read the generated file from the drive
as you can see
if I don't set the value to -1 to READDATA (*MemoryID,gigs-1)
READDATA don't work (dead)
and
if I don't set the value to -2 at end of the block, the result is always ' zero ' (*MemoryID+gigs-2)
while with the gigs-2 I read the 'nine' value on all the cells.

Thank you

Code: Select all

Global.q gigs = 4*1024*1024*1024 , *MemoryID
Global.b mybyte

*MemoryID = AllocateMemory(gigs)
If *MemoryID > 0
FillMemory(*MemoryID,gigs,9,#PB_Byte)   ;write 9 to all area
If CreateFile(0,"test.txt")
FileBuffersSize(0,#MAXWORD)
*writeptr=*MemoryID
*end = *MemoryID + gigs
While *writeptr < *end
If *end-*writeptr >= #MAXWORD
chunksize = #MAXWORD
Else 
chunksize = *end-*writeptr
EndIf
WriteData(0, *writeptr, chunksize)
*writeptr + chunksize
Wend
FlushFileBuffers(0) 
CloseFile(0) 
EndIf
EndIf

FreeMemory(*MemoryID)

If OpenFile(0,"test.txt")
FileBuffersSize(0,#MAXWORD)
gigs=Lof(0)
*MemoryID = AllocateMemory(gigs)
ReadData(0,*MemoryID,gigs-1)    ; works only if gigs is gigs-1, otherwise not read and load  any to memory
CloseFile(0)
EndIf
mybyte = PeekB(*MemoryID+gigs-2)    ;; read  ' 9 ' only if gigs is gigs-2 , otherwise 'gigs-1 = 0'  or 'gigs = 0'
Debug mybyte
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: WriteData and Invalid memory access

Post by NicTheQuick »

My guess is that Purebasic uses `openat` instead of `openat64` for opening files which can be seen in a trace:

Code: Select all

nicolas@Rocky:~/tmp$ strace ./4gb_file 
execve("./4gb_file", ["./4gb_file"], 0x7ffd22a30e70 /* 69 vars */) = 0
brk(NULL)                               = 0x55dee855a000
arch_prctl(0x3001 /* ARCH_??? */, 0x7ffeb512e2b0) = -1 EINVAL (Das Argument ist ungültig)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (Datei oder Verzeichnis nicht gefunden)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=192205, ...}, AT_EMPTY_PATH) = 0
mmap(NULL, 192205, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fdfbc900000
close(3)                                = 0
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000\342\2\0\0\0\0\0"..., 832) = 832
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0"..., 48, 848) = 48
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\270\3{b`\206SF\200#!\335\"V\270\255"..., 68, 896) = 68
newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=2215936, ...}, AT_EMPTY_PATH) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fdfbc8fe000
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
mmap(NULL, 2260144, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fdfbc6d6000
mprotect(0x7fdfbc702000, 2002944, PROT_NONE) = 0
mmap(0x7fdfbc702000, 1654784, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2c000) = 0x7fdfbc702000
mmap(0x7fdfbc896000, 344064, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7fdfbc896000
mmap(0x7fdfbc8eb000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x214000) = 0x7fdfbc8eb000
mmap(0x7fdfbc8f1000, 52400, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fdfbc8f1000
close(3)                                = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fdfbc6d4000
arch_prctl(ARCH_SET_FS, 0x7fdfbc8ff5c0) = 0
set_tid_address(0x7fdfbc8ff890)         = 1019221
set_robust_list(0x7fdfbc8ff8a0, 24)     = 0
mprotect(0x7fdfbc8eb000, 12288, PROT_READ) = 0
mprotect(0x55dee70d6000, 4096, PROT_READ|PROT_WRITE) = 0
mprotect(0x55dee70d7000, 16384, PROT_READ|PROT_WRITE|PROT_EXEC) = 0
mprotect(0x55dee70db000, 4096, PROT_READ|PROT_WRITE) = 0
mprotect(0x55dee70db000, 4096, PROT_READ) = 0
mprotect(0x55dee70d7000, 16384, PROT_READ|PROT_EXEC) = 0
mprotect(0x55dee70d6000, 4096, PROT_READ) = 0
mprotect(0x55dee70dc000, 4096, PROT_READ) = 0
mprotect(0x7fdfbc962000, 8192, PROT_READ) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
munmap(0x7fdfbc900000, 192205)          = 0
getrandom("\x57\x40\x4d\x89\x3b\x83\xac\x3a", 8, GRND_NONBLOCK) = 8
brk(NULL)                               = 0x55dee855a000
brk(0x55dee857b000)                     = 0x55dee857b000
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=6401952, ...}, AT_EMPTY_PATH) = 0
mmap(NULL, 6401952, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fdfbc0b9000
close(3)                                = 0
mmap(NULL, 4194308096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fdec20b8000

openat(AT_FDCWD, "/home/nicolas/Text.txt", O_RDWR|O_CREAT|O_TRUNC, 0666) = 3
mmap(NULL, 135168, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fdfbc90e000
newfstatat(3, "", {st_mode=S_IFREG|0664, st_size=0, ...}, AT_EMPTY_PATH) = 0
lseek(3, 0, SEEK_CUR)                   = 0
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x7fdebc0b8008} ---
+++ killed by SIGSEGV +++
Speicherzugriffsfehler
The proper function should be `openat64` I guess.
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.
Post Reply