It is currently Thu Apr 02, 2020 3:58 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: [PB5.41-64] Memory error
PostPosted: Fri Dec 25, 2015 11:08 am 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2476
When running the following code under Purebasic 5.41-64, the trim function cproduces an memory error in this line: s.s=Trim(ReadString(0)).

Code:
; Define

   EnableExplicit

   Structure PenType
      Color.i
      Width.i
   EndStructure

   Global PenCounter
   Global Dim Pen.PenType(0)

; EndDefine

Procedure load()

   Protected s.s,t.s
   Protected name.s
   Protected code.s,header.s
   Protected value.s,type.s
   Protected n,m,z,f1,f2,x,y

   If ReadFile(0,"Test.svg")
      While Not(Eof(0))
         s.s=Trim(ReadString(0))
         Debug s

         If PeekA(@s)='.'
            s=LCase(s);                           .str0 {stroke:#2B2A29;stroke-width:20}
            name=Mid(StringField(s,1," "),2);            str0
            Select Left(name,3)
            Case "str";                           .str0
               m=Val(Mid(name,4))
               If m>PenCounter
                  PenCounter=m
                  ReDim Pen(PenCounter)
               EndIf
            EndSelect
         EndIf

      Wend

      CloseFile(0)
      ProcedureReturn #True

   Else
      Debug "File not found."
      Debug GetCurrentDirectory()

   EndIf

EndProcedure

If load()
   Debug "Ok"
EndIf



Test.svg:
    <defs>
    <style type="text/css">
    <![CDATA[
    .str0 {stroke:#FEFEFE;stroke-width:7.62}
    .str1 {stroke:#EBECEC;stroke-width:7.62}
    .str3 {stroke:#ABD208;stroke-width:7.62}
    .str6 {stroke:#F5DF25;stroke-width:7.62}
    .str2 {stroke:#F69301;stroke-width:7.62}
    .str5 {stroke:#FBF09D;stroke-width:7.62}
    .str4 {stroke:#2F68B2;stroke-width:7.62}
    .fil0 {fill:#FEFEFE}
    .fil2 {fill:#ABD208}
    .fil5 {fill:#F5DF25}
    .fil1 {fill:#F69301}
    .fil4 {fill:#FBF09D}
    .fil3 {fill:#2F68B2}
    ]]>
    </style>
    </defs>
    </svg>


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Fri Dec 25, 2015 3:27 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4587
Location: Germany
Compiled in unicode or ascii ?


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Sat Dec 26, 2015 8:17 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2476
infratec wrote:
Compiled in unicode or ascii ?

unicode and ascii are showing the same problem - but only in the 64 bit version of purebasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Sun Dec 27, 2015 2:45 am 
Offline
Enthusiast
Enthusiast

Joined: Sun Feb 22, 2015 2:33 am
Posts: 139
Hi Michael Vogel.
Your code when compiled with 64-bit PureBasic 5.41 LTS and when run on a Windows 7 Home Premium 64-bit OS did not produce any errors. The following Debug output was produced:

<defs>
<style type="text/css">
<![CDATA[
.str0 {stroke:#FEFEFE;stroke-width:7.62}
.str1 {stroke:#EBECEC;stroke-width:7.62}
.str3 {stroke:#ABD208;stroke-width:7.62}
.str6 {stroke:#F5DF25;stroke-width:7.62}
.str2 {stroke:#F69301;stroke-width:7.62}
.str5 {stroke:#FBF09D;stroke-width:7.62}
.str4 {stroke:#2F68B2;stroke-width:7.62}
.fil0 {fill:#FEFEFE}
.fil2 {fill:#ABD208}
.fil5 {fill:#F5DF25}
.fil1 {fill:#F69301}
.fil4 {fill:#FBF09D}
.fil3 {fill:#2F68B2}
]]>
</style>
</defs>
</svg>
Ok

_________________
Think Unicode!


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Sun Dec 27, 2015 10:09 am 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2476
Thank you for testing, ElementE!

Forgot to say, that the file "test.svg" has to be saved in ASCII, otherwise there's no problem. I've uploaded the original file now for testing.

The output stops here with ".str1 {stroke:#EBECEC;stroke-width:7.62}" before the memory error willl occur (tested with Windows 8.1).


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Sun Dec 27, 2015 11:41 am 
Offline
Addict
Addict

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3796
Location: Berlin, Germany
Hi,

I can confirm the bug with the x64 versions of both PB 5.31 and PB 5.41 LTS on Windows 10.
Here is simplified code that still demonstrates the bug (memory error at the line Case "str"):

Code:
EnableExplicit

Define s.s, m.i, PenCounter.i
Dim Pen.i(0)

Restore MyData

Read.s s
While s <> ""
   Debug s
   
   Select Left(s, 3)
      Case "str"
         m = Val(Mid(s, 4))
         If m > PenCounter
            PenCounter = m
            ReDim Pen(PenCounter)
         EndIf
   EndSelect
   
   Read.s s
Wend


DataSection
   MyData:
   Data.s "str0"
   Data.s "str1"
   Data.s "str2"
   Data.s "str3"
   Data.s ""
EndDataSection

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Sun Dec 27, 2015 8:39 pm 
Offline
Enthusiast
Enthusiast

Joined: Sun Feb 22, 2015 2:33 am
Posts: 139
I still cannot reproduce the error.
I ran Little John's code and got the following output (with no errors):

str0
str1
str2
str3

Tested using PB v5.41 LTS (64-bit) on Windows 7 Home Premium (64-bit)
Little John's code:
Code:
EnableExplicit

Define s.s, m.i, PenCounter.i
Dim Pen.i(0)

Restore MyData

Read.s s
While s <> ""
   Debug s
   
   Select Left(s, 3)
      Case "str"
         m = Val(Mid(s, 4))
         If m > PenCounter
            PenCounter = m
            ReDim Pen(PenCounter)
         EndIf
   EndSelect
   
   Read.s s
Wend


DataSection
   MyData:
   Data.s "str0"
   Data.s "str1"
   Data.s "str2"
   Data.s "str3"
   Data.s ""
EndDataSection

_________________
Think Unicode!


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Sun Dec 27, 2015 11:48 pm 
Offline
User
User

Joined: Wed May 13, 2009 8:38 am
Posts: 84
Location: Arizona, USA
Confirmed Windows 10 Professional x64 PureBasic 5.31 and 5.41 x64. Appears to be Windows 10 x64 only.

Quote:
[15:42:38] str0
[15:42:38] str
[15:42:38] str1
[15:42:38] str
[15:42:38] str2

[15:42:38] Waiting for executable to start...
[15:42:38] Executable type: Windows - x64 (64bit, Unicode)
[15:42:38] Executable started.
[15:42:38] [ERROR] Line: 13
[15:42:38] [ERROR] Invalid memory access. (read error at address 5441626176)


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Mon Dec 28, 2015 3:19 am 
Offline
Enthusiast
Enthusiast

Joined: Sun Feb 22, 2015 2:33 am
Posts: 139
Confirmed that run-time Error is raised under 64-bit Windows 10!

The following code will also cause an error:
Code:
; Generates Error under following conditions:
;   (1) compiled using PureBasic 64-bit compiler
;   AND
;   (2) program executed under 64-bit Windows 10

EnableExplicit
Define s.s, k.i, PenCounter.i
Dim Pen.i(0)

s="str"

For k=0 To 100
  Debug k
  Select Left(s,3)
    Case "str"
      If k > PenCounter
        PenCounter = k
        ReDim Pen(PenCounter)
      EndIf
  EndSelect
Next

The problem seems to be that in 64-bit code under Windows 10 there is memory management conflict when using both the"Left(s,3)" and "ReDim(PenCounter)" code items.

The strange thing is that, under the same conditions, the following code DOES work (no error is raised).
Code:
; No error raised under following conditions:
;   (1) compiled using PureBasic 64-bit compiler
;   AND
;   (2) program executed under 64-bit Windows 10

EnableExplicit
Define s.s, k.i, PenCounter.i
Dim Pen.i(0)
Define temp$

s="str"

For k=0 To 100
  Debug k
  temp$=Left(s,3)
  Select Left(s,3)
    Case "str"
      If k > PenCounter
        PenCounter = k
        ReDim Pen(PenCounter)
      EndIf
  EndSelect
Next

Notice that the temp$ is not even used in the Select statement!

_________________
Think Unicode!


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Mon Dec 28, 2015 3:38 am 
Offline
Enthusiast
Enthusiast

Joined: Sun Feb 22, 2015 2:33 am
Posts: 139
I am going to guess that a change was made in the 64-bit run-time libraries of Windows 8 and 10 that results in the code not working.

_________________
Think Unicode!


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Mon Dec 28, 2015 12:28 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Feb 04, 2013 5:28 pm
Posts: 333
Code:
; Generates Error under following conditions:
;   (1) compiled using PureBasic 64-bit compiler
;   AND
;   (2) program executed under 64-bit Windows 10

EnableExplicit
Define s.s, k.i
Dim Pen.i(0)

s = "hello"

For k = 0 To 10
  Select Trim(s)
    Case "hello"     
      ReDim Pen(k)
      Debug k ; executed once
  EndSelect
Next



This crashes here on the second loop (PB 5.40 / W10 x64)
The two things I had to left in place to make it crash are the string function call after the Select, and the Redim.


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Mon Dec 28, 2015 12:49 pm 
Offline
Enthusiast
Enthusiast

Joined: Sun Feb 22, 2015 2:33 am
Posts: 139
Quote:
The two things I had to left in place to make it crash are the string function call after the Select, and the Redim.

Yes, and if "ReDim" is replaced with "Dim" no error is raised.
This is very mysterious.

_________________
Think Unicode!


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Tue Dec 29, 2015 12:14 am 
Offline
Enthusiast
Enthusiast

Joined: Sun Feb 22, 2015 2:33 am
Posts: 139
With the addition of some inline assembly code, no error is raised under Windows 10 (64-bit).
If the assembly code is commented out, and error will be raised.
On a Windows 7 (64-bit) system, the assembly code is not required and the program runs without error.
Can someone explain why this is so?
Code:
EnableExplicit
EnableASM

Define s.s, k.i
Dim Pen.i(0)
Define temp$

s="str"

For k=0 To 10
  Debug k
 
  ;temp$=s
   ! extrn SYS_CopyString
   ! MOV    rcx,qword [v_s]
   ! PUSH   qword [PB_StringBasePosition]
   ! SUB    rsp,40
   ! CALL   SYS_CopyString
   ! ADD    rsp,40
   ! LEA    rcx,[v_temp$]
   ! POP    rdx
   ! CALL   SYS_AllocateString4
 
  Select Trim(s)
    Case "str"
      ReDim Pen(k)
  EndSelect
Next

_________________
Think Unicode!


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Tue Dec 29, 2015 10:06 am 
Offline
Enthusiast
Enthusiast

Joined: Sun Feb 22, 2015 2:33 am
Posts: 139
The following code works on 64-bit Windows 10.
Compiled using the PureBasic v5.41 LTS x64 compiler.
Code:
EnableExplicit
EnableASM

Define s.s, k.i
Dim Pen.i(0)

s="str"
For k=0 To 10
  Debug k
  Select Trim(s)
    Case "str"
      ! PUSH rax
      ReDim Pen(k)
      ! POP rax
  EndSelect
Next
On a Windows 10 x64 system if a PB string function is used in a Select expression, and if the ReDim function is called in one of the Select cases, then saving and restoring the rax register around the ReDim function call will allow the code to run without error.

This is still mysterious, perhaps someone here can explain the details as to what is happening.

_________________
Think Unicode!


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB5.41-64] Memory error
PostPosted: Sat Jan 02, 2016 10:41 pm 
Offline
Enthusiast
Enthusiast

Joined: Sun Feb 22, 2015 2:33 am
Posts: 139
I am still looking at this as I am concerned about PureBasic code working on a Windows 10 x64 system.

This code works under win10 x64.
Code:
EnableExplicit
EnableASM

Define s.s, k.i
Dim Pen.i(0)

s="str"
For k=0 To 10
  Debug k
  Select Trim(s)
    Case "str"
      ! SUB rsp,1
      ReDim Pen(k)
      ! ADD rsp,1
  EndSelect
Next

This indicates the the issue involves maintaining the stack when calling ReDim.
Why Windows 10 requires extra space on the stack when calling ReDim is unknown.

_________________
Think Unicode!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye