[solved] "old" code crashed

General FreeBASIC programming questions.
Post Reply
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

[solved] "old" code crashed

Post by D.J.Peters »

this "old" code crashed on exit (win10 64-bit)

Joshy
gdb wrote:Reading symbols from .\cloth.exe...done.
(gdb) r
Starting program: D:\done\cloth.exe
[New Thread 10612.0xdb0]
[New Thread 10612.0x1fd8]
[New Thread 10612.0x14ec]
[New Thread 10612.0x219c]
[New Thread 10612.0x2290]
[Thread 10612.0x2290 exited with code 0]
[Thread 10612.0x219c exited with code 1]
[New Thread 10612.0x25c8]
[New Thread 10612.0x2760]
[New Thread 10612.0x784]
[Thread 10612.0x784 exited with code 0]
[Thread 10612.0x2760 exited with code 1]
[New Thread 10612.0x28ec]
[Thread 10612.0x28ec exited with code 1]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffb094f0e57 in ntdll!RtlFreeHeap () from C:\WINDOWS\SYSTEM32\ntdll.dll

Code: Select all

#include "fbgfx.bi"

#ifndef __FB_64BIT__
Type REAL As single
#else
Type REAL As double
#endif

'Randomize Timer
#define scr_w  1024
#define scr_h  756
#define scr_b   32 ' 8,15,16,24 or 32

#if (scr_b=8)
  Type pixel As Ubyte
  #undef RGB
  #define RGB(r,g,b) ((r And &HE0) Or ((g And &HE0) Shr 3) Or ((b And &HC0) Shr 6))
  Sub Palette332()
    Dim As Integer i,r,g,b
    For i = 0 To 255
      r=(((i Shr 5) And &H07) * 255) / 7
      g=(((i Shr 2) And &H07) * 255) / 7
      b=(((i Shr 0) And &H03) * 255) / 3
      Palette i,r,g,b
    Next
  End Sub 
#elseif (scr_b=15) Or (scr_b=16)
  Type pixel As Ushort
  #undef RGB
  #define RGB(r,g,b) (((r Shr 3) Shl 11) Or ((g Shr 2) Shl 5) Or (b Shr 3))
#elseif (scr_b=24) Or (scr_b=32)
  Type pixel As Ulong
#else
  #error 666: bits per pixel (scr_b) must be 8,15,16,24 Or 32
#endif


#define SHIFTS   8 ' 24:8 fixed point format

Type vector2d
  As Integer x,y
End Type
Sub triangle(d   As pixel Ptr, _
             p() As vector2d , _
             c   As pixel    ) ' color

  dim as integer   t =Any,b=Any,l=Any,r=Any
  dim as integer   d1 =Any,d2=Any,s1=Any,s2=Any
  dim as pixel ptr row=Any,cstart=Any,cend=Any
  dim as vector2d  v0 =Any,v1=Any,v2=Any

  v0=p(0):v1=p(1):v2=p(2)

  If (v1.y>v2.y) then swap v1,v2
  If (v0.y>v2.y) then swap v0,v2
  If (v0.y>v1.y) then swap v0,v1
  If (v2.y=v0.y) then return

  s1=((v2.x-v0.x) Shl SHIFTS)/(v2.y-v0.y)
  d1=v0.x Shl SHIFTS
  For i As Integer=0 To 1
    s2=((v1.x-v0.x) Shl SHIFTS)/(v1.y-v0.y)
    d2=v0.x Shl SHIFTS
    t=v0.y ' top
    ' begin in first row (top=0)
    If t<0 Then d1-=s1*t : d2-=s2*t : t=0
    b=v1.y ' bottom
    ' end in last row (b=scr_h-1)
    If b>=scr_h Then b=scr_h-1
    
    If b<=t    Then Goto next_triangle
    
    row=d+t*scr_w ' first row
    b-=t ' how many scanlines
    
    While b  ' from top to bottom
      l=d1 Shr SHIFTS:r=d2 Shr SHIFTS
      If l>r      Then Swap l,r
      If l>=scr_w Then Goto next_scanline
      If r<1      Then Goto next_scanline
      If r>=scr_w Then r=scr_w-1
      cstart=row+l ' first pixel
      cend  =row+r ' last  pixel
      While cstart<cend:*cstart=c:cstart+=1:Wend
      next_scanline:
      d1+=s1:d2+=s2:row+=scr_w:b-=1
    Wend
    next_triangle:
    d1= (v0.x Shl SHIFTS)+((v1.y-v0.y)*s1)
    v0=v1:v1=v2
  Next
End Sub

const cells      as Integer = 100
const nTriangles as Integer = cells*cells*2
const stiffnes   as REAL   =  200
const gravity    as REAL   = -9.81
const DT         as REAL   = 1/30
const w          as REAL   = scr_w/(cells-2)
const h          as REAL   = scr_h/(cells*2)
const wm         as REAL   = scr_w/2
const hm         as REAL   = scr_h/2


Type vector3d
  As REAL     x,y,z
End Type
Type POINT3D
  As VECTOR3D  p,v,f
  As BOOLEAN   fixed
  As REAL     mass,e
End Type

Dim Shared As POINT3D  points (cells-1,cells-1)


Sub CreateIt()
  For x As Integer=0 To cells-1 step (cells-1)\5
    points(x,0).fixed=true
  next

  For y As Integer=0 To cells-1
    For x As Integer=0 To cells-1
      With points(x,y)
        .p.x=-wm+x*w
        .p.y=hm
        .mass=1+rnd
      End With
    Next
  Next
End Sub

Sub CalcForces()
  Dim As VECTOR3D Fd,Vd,F
  Dim As REAL force,delta,direction,l2,air
  Static As REAL sw
  air=Sin(sw) ' *(5+Rnd*5)
  sw+=0.1*dt
  ' forces x,y
  For y As Integer=0 To cells-1
    For x As Integer=0 To cells-1
      points(x,y).f.x = air'*Rnd
      points(x,y).f.y = gravity * points(x,y).mass
    Next
  Next

  For y As Integer=0 To cells-1
    For x As Integer=0 To cells-2
      With points(x+1,y)
      Fd.x=points(x,y).p.x-.p.x
      Fd.y=points(x,y).p.y-.p.y
      l2=fd.x*fd.x + fd.y*fd.y
      If l2 Then
        l2=Sqr(l2)
        delta=l2-w
        force=delta*stiffnes
        vd.x = points(x,y).v.x-.v.x
        vd.y = points(x,y).v.y-.v.y
        force+=(vd.x*fd.x + vd.y*fd.y)/l2
        fd.x/=l2:fd.y/=l2
        f.x=fd.x*force
        f.y=fd.y*force
        points(x,y).f.x-=f.x
        points(x,y).f.y-=f.y
        .f.x+=f.x
        .f.y+=f.y
      End If
      End With
    Next
  Next


  For y As Integer=0 To cells-2
    For x As Integer=0 To cells-1
      With points(x,y+1)
      Fd.x=points(x,y).p.x-.p.x
      Fd.y=points(x,y).p.y-.p.y
      l2=fd.x*fd.x + fd.y*fd.y
      If l2 Then
        l2=Sqr(l2)
        delta=l2-h
        force=delta*stiffnes
        vd.x = points(x,y).v.x-.v.x
        vd.y = points(x,y).v.y-.v.y
        force+=(vd.x*fd.x + vd.y*fd.y)/l2
        fd.x/=l2:fd.y/=l2
        f.x=fd.x*force
        f.y=fd.y*force
        points(x,y).f.x-=f.x
        points(x,y).f.y-=f.y
        .f.x+=f.x
        .f.y+=f.y
      End If
      End With
    Next
  Next

  For y As Integer=0 To cells-1
    For x As Integer=0 To cells-1
      With points(x,y)
        If .fixed=0 Then
          .v.x+= .f.x * DT ' velocity + force
          .v.y+= .f.y * DT
          .p.x+= .v.x * DT ' position + velocity
          .p.y+= .v.y * DT
          If .p.y<1-(scr_h\2) Then .v.y*=-0.999
          .v.x*=0.999
          
        End If
      End With
    Next
  Next
End Sub

Sub DrawFilledTriangles()
  static As vector2d t(2)
  dim    As pixel    c
  dim    As Integer  i,j
  dim    As pixel Ptr pixels=screenptr
  For y As Integer=0 To cells-2
    i=(y and 1)
    For x As Integer=0 To cells-2
      t(0).x=wm+points(x  ,y  ).p.x
      t(0).y=hm-points(x  ,y  ).p.y
      t(1).x=wm+points(x+1,y  ).p.x
      t(1).y=hm-points(x+1,y  ).p.y
      t(2).x=wm+points(x+1,y+1).p.x
      t(2).y=hm-points(x+1,y+1).p.y
      dim as REAL rr=points(x,y).f.x + points(x+1,y).f.x + points(x+1,y).f.x + points(x+1,y+1).f.x + points(x,y+1).f.x
      rr=sqr(rr)
      dim as REAL gg=points(x,y).f.y + points(x+1,y).f.y + points(x+1,y).f.y + points(x+1,y+1).f.y + points(x,y+1).f.y
      gg=sqr(gg)
     
      dim as REAL bb=points(x,y).v.x*points(x,y).v.y
      bb+=points(x+1,y  ).v.x*points(x+1,y  ).v.y
      bb+=points(x+1,y+1).v.x*points(x+1,y+1).v.y
      bb+=points(x  ,y+1).v.x*points(x  ,y+1).v.y
      bb=sqr(bb)
     
      dim as integer r=rr*8:if r>255 then r=255
      dim as integer g=gg*8:if g>255 then g=255
      dim as integer b=bb*8:if b>255 then b=255
      j=(x and 1)
      If i xor j then
        r\=2:g\=2:b\=2
      End If
      c=rgb(r,g,b)
      Triangle pixels,t(),c
      t(1).x=wm+points(x,y+1).p.x
      t(1).y=hm-points(x,y+1).p.y
      Triangle pixels,t(),c
    Next
  Next
End Sub


'
'main
'
dim as integer frames,fps
dim as double t1,t2

ScreenRes scr_w,scr_h,scr_b
#if (scr_b=8)
  Palette332
#endif

CreateIt

t1=Timer
While inkey()=""
  CalcForces
  ScreenLock
    cls : DrawFilledTriangles
  ScreenUnlock

  frames+=1
  If frames mod 50=0 Then
    t2=Timer: fps=50/(t2-t1) : t1=t2
    WindowTitle nTriangles & " triangles FPS=" & Str(fps) & " [ESC] = quit"
  End If
Wend

Last edited by D.J.Peters on Sep 28, 2017 19:53, edited 1 time in total.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: "old" code crashed

Post by dkl »

Happens also on Linux, 32bit or 64bit, git version or 1.05.0:
==4994== Invalid free() / delete / delete[] / realloc()
==4994== at 0x4C2ED5B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4994== by 0x40708C: release_gfx_mem (gfx_screen.c:70)
==4994== by 0x40708C: set_mode (gfx_screen.c:189)
==4994== by 0x407976: exit_proc (gfx_screen.c:100)
==4994== by 0x413325: fb_End (init.c:116)
==4994== by 0x406348: main (1.bas:283)
==4994== Address 0xff007f0006cfb0c0 is not stack'd, malloc'd or (recently) free'd
Looks like memory corruption, or a bug in FB's gfxlib.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: "old" code crashed

Post by dodicat »

In sub createit, if you set
.p.y=hm-5
It stops the crash.
I was thinking perhaps the thing dangling from points at exactly y=0 might put some pixels off screen by a rounding errror
But maybe this is not the reason.
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: "old" code crashed

Post by fxm »

dkl wrote:Looks like memory corruption, or a bug in FB's gfxlib.
Yes, see my correction on line 79:

Code: Select all

#include "fbgfx.bi"

#ifndef __FB_64BIT__
Type REAL As single
#else
Type REAL As double
#endif

'Randomize Timer
#define scr_w  1024
#define scr_h  756
#define scr_b   32 ' 8,15,16,24 or 32

#if (scr_b=8)
  Type pixel As Ubyte
  #undef RGB
  #define RGB(r,g,b) ((r And &HE0) Or ((g And &HE0) Shr 3) Or ((b And &HC0) Shr 6))
  Sub Palette332()
    Dim As Integer i,r,g,b
    For i = 0 To 255
      r=(((i Shr 5) And &H07) * 255) / 7
      g=(((i Shr 2) And &H07) * 255) / 7
      b=(((i Shr 0) And &H03) * 255) / 3
      Palette i,r,g,b
    Next
  End Sub
#elseif (scr_b=15) Or (scr_b=16)
  Type pixel As Ushort
  #undef RGB
  #define RGB(r,g,b) (((r Shr 3) Shl 11) Or ((g Shr 2) Shl 5) Or (b Shr 3))
#elseif (scr_b=24) Or (scr_b=32)
  Type pixel As Ulong
#else
  #error 666: bits per pixel (scr_b) must be 8,15,16,24 Or 32
#endif


#define SHIFTS   8 ' 24:8 fixed point format

Type vector2d
  As Integer x,y
End Type
Sub triangle(d   As pixel Ptr, _
             p() As vector2d , _
             c   As pixel    ) ' color

  dim as integer   t =Any,b=Any,l=Any,r=Any
  dim as integer   d1 =Any,d2=Any,s1=Any,s2=Any
  dim as pixel ptr row=Any,cstart=Any,cend=Any
  dim as vector2d  v0 =Any,v1=Any,v2=Any

  v0=p(0):v1=p(1):v2=p(2)

  If (v1.y>v2.y) then swap v1,v2
  If (v0.y>v2.y) then swap v0,v2
  If (v0.y>v1.y) then swap v0,v1
  If (v2.y=v0.y) then return

  s1=((v2.x-v0.x) Shl SHIFTS)/(v2.y-v0.y)
  d1=v0.x Shl SHIFTS
  For i As Integer=0 To 1
    s2=((v1.x-v0.x) Shl SHIFTS)/(v1.y-v0.y)
    d2=v0.x Shl SHIFTS
    t=v0.y ' top
    ' begin in first row (top=0)
    If t<0 Then d1-=s1*t : d2-=s2*t : t=0
    b=v1.y ' bottom
    ' end in last row (b=scr_h-1)
    If b>=scr_h Then b=scr_h-1
   
    If b<=t    Then Goto next_triangle
   
    row=d+t*scr_w ' first row
    b-=t ' how many scanlines
   
    While b  ' from top to bottom
      l=d1 Shr SHIFTS:r=d2 Shr SHIFTS
      If l>r      Then Swap l,r
'      If l>=scr_w Then Goto next_scanline
      If l>=scr_w Or l<0 Then Goto next_scanline  '' after successive shiftings, "l" may also be negative (a lot of "-1" at starting)
      If r<1      Then Goto next_scanline
      If r>=scr_w Then r=scr_w-1
      cstart=row+l ' first pixel
      cend  =row+r ' last  pixel
      While cstart<cend:*cstart=c:cstart+=1:Wend
      next_scanline:
      d1+=s1:d2+=s2:row+=scr_w:b-=1
    Wend
    next_triangle:
    d1= (v0.x Shl SHIFTS)+((v1.y-v0.y)*s1)
    v0=v1:v1=v2
  Next
End Sub

const cells      as Integer = 100
const nTriangles as Integer = cells*cells*2
const stiffnes   as REAL   =  200
const gravity    as REAL   = -9.81
const DT         as REAL   = 1/30
const w          as REAL   = scr_w/(cells-2)
const h          as REAL   = scr_h/(cells*2)
const wm         as REAL   = scr_w/2
const hm         as REAL   = scr_h/2


Type vector3d
  As REAL     x,y,z
End Type
Type POINT3D
  As VECTOR3D  p,v,f
  As BOOLEAN   fixed
  As REAL     mass,e
End Type

Dim Shared As POINT3D  points (cells-1,cells-1)


Sub CreateIt()
  For x As Integer=0 To cells-1 step (cells-1)\5
    points(x,0).fixed=true
  next

  For y As Integer=0 To cells-1
    For x As Integer=0 To cells-1
      With points(x,y)
        .p.x=-wm+x*w
        .p.y=hm
        .mass=1+rnd
      End With
    Next
  Next
End Sub

Sub CalcForces()
  Dim As VECTOR3D Fd,Vd,F
  Dim As REAL force,delta,direction,l2,air
  Static As REAL sw
  air=Sin(sw) ' *(5+Rnd*5)
  sw+=0.1*dt
  ' forces x,y
  For y As Integer=0 To cells-1
    For x As Integer=0 To cells-1
      points(x,y).f.x = air'*Rnd
      points(x,y).f.y = gravity * points(x,y).mass
    Next
  Next

  For y As Integer=0 To cells-1
    For x As Integer=0 To cells-2
      With points(x+1,y)
      Fd.x=points(x,y).p.x-.p.x
      Fd.y=points(x,y).p.y-.p.y
      l2=fd.x*fd.x + fd.y*fd.y
      If l2 Then
        l2=Sqr(l2)
        delta=l2-w
        force=delta*stiffnes
        vd.x = points(x,y).v.x-.v.x
        vd.y = points(x,y).v.y-.v.y
        force+=(vd.x*fd.x + vd.y*fd.y)/l2
        fd.x/=l2:fd.y/=l2
        f.x=fd.x*force
        f.y=fd.y*force
        points(x,y).f.x-=f.x
        points(x,y).f.y-=f.y
        .f.x+=f.x
        .f.y+=f.y
      End If
      End With
    Next
  Next


  For y As Integer=0 To cells-2
    For x As Integer=0 To cells-1
      With points(x,y+1)
      Fd.x=points(x,y).p.x-.p.x
      Fd.y=points(x,y).p.y-.p.y
      l2=fd.x*fd.x + fd.y*fd.y
      If l2 Then
        l2=Sqr(l2)
        delta=l2-h
        force=delta*stiffnes
        vd.x = points(x,y).v.x-.v.x
        vd.y = points(x,y).v.y-.v.y
        force+=(vd.x*fd.x + vd.y*fd.y)/l2
        fd.x/=l2:fd.y/=l2
        f.x=fd.x*force
        f.y=fd.y*force
        points(x,y).f.x-=f.x
        points(x,y).f.y-=f.y
        .f.x+=f.x
        .f.y+=f.y
      End If
      End With
    Next
  Next

  For y As Integer=0 To cells-1
    For x As Integer=0 To cells-1
      With points(x,y)
        If .fixed=0 Then
          .v.x+= .f.x * DT ' velocity + force
          .v.y+= .f.y * DT
          .p.x+= .v.x * DT ' position + velocity
          .p.y+= .v.y * DT
          If .p.y<1-(scr_h\2) Then .v.y*=-0.999
          .v.x*=0.999
         
        End If
      End With
    Next
  Next
End Sub

Sub DrawFilledTriangles()
  static As vector2d t(2)
  dim    As pixel    c
  dim    As Integer  i,j
  dim    As pixel Ptr pixels=screenptr
  For y As Integer=0 To cells-2
    i=(y and 1)
    For x As Integer=0 To cells-2
      t(0).x=wm+points(x  ,y  ).p.x
      t(0).y=hm-points(x  ,y  ).p.y
      t(1).x=wm+points(x+1,y  ).p.x
      t(1).y=hm-points(x+1,y  ).p.y
      t(2).x=wm+points(x+1,y+1).p.x
      t(2).y=hm-points(x+1,y+1).p.y
      dim as REAL rr=points(x,y).f.x + points(x+1,y).f.x + points(x+1,y).f.x + points(x+1,y+1).f.x + points(x,y+1).f.x
      rr=sqr(rr)
      dim as REAL gg=points(x,y).f.y + points(x+1,y).f.y + points(x+1,y).f.y + points(x+1,y+1).f.y + points(x,y+1).f.y
      gg=sqr(gg)
     
      dim as REAL bb=points(x,y).v.x*points(x,y).v.y
      bb+=points(x+1,y  ).v.x*points(x+1,y  ).v.y
      bb+=points(x+1,y+1).v.x*points(x+1,y+1).v.y
      bb+=points(x  ,y+1).v.x*points(x  ,y+1).v.y
      bb=sqr(bb)
     
      dim as integer r=rr*8:if r>255 then r=255
      dim as integer g=gg*8:if g>255 then g=255
      dim as integer b=bb*8:if b>255 then b=255
      j=(x and 1)
      If i xor j then
        r\=2:g\=2:b\=2
      End If
      c=rgb(r,g,b)
      Triangle pixels,t(),c
      t(1).x=wm+points(x,y+1).p.x
      t(1).y=hm-points(x,y+1).p.y
      Triangle pixels,t(),c
    Next
  Next
End Sub


'
'main
'
dim as integer frames,fps
dim as double t1,t2

ScreenRes scr_w,scr_h,scr_b
#if (scr_b=8)
  Palette332
#endif

CreateIt

t1=Timer
While inkey()=""
  CalcForces
  ScreenLock
    cls : DrawFilledTriangles
  ScreenUnlock

  frames+=1
  If frames mod 50=0 Then
    t2=Timer: fps=50/(t2-t1) : t1=t2
    WindowTitle nTriangles & " triangles FPS=" & Str(fps) & " [ESC] = quit"
  End If
Wend
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: "old" code crashed

Post by D.J.Peters »

@fxm my fault good find
I can't remember why I coment out the less than zero test.

I wonder me why it crash on exit not while rendering
pointers are unsigned and a negative l value in "cstart=row+l"
shoud be an address far outside of the allocated screen buffer.

How ever thank you all.

Joshy
fxm
Moderator
Posts: 12108
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: [solved] "old" code crashed

Post by fxm »

With l<0, 'cstart' becomes < 'ScreenPtr', and '*cstart=c' writes outside the frame buffer, but the bad values of 'l' remain very small negative values.
Post Reply