Heater
Posts: 13685
Joined: Tue Jul 17, 2012 3:02 pm

Re: ScriptBasic

Wed Jun 19, 2019 5:11 am

"OSBASIC" is a terrible name. I suggest calling it YAFSB :)

Going platform specific is a terrible idea. Even if that platform is the Pi. Not being cross-platform would severely limit it's attraction, that after all is a lot of the point of a high level language. It would annoy any existing (not)ScriptBasic users on other platforms.

Perhaps timeless, but still a new kid on the block. C is nearly 50 years old, Python 30 years...

Now I have a dilemma. If what I thought was ScriptBasic is not actually ScriptBasic I have to rethink the whole (not)ScriptBasic fibo challenge entry.
Memory in C++ is a leaky abstraction .

jahboater
Posts: 4772
Joined: Wed Feb 04, 2015 6:38 pm

Re: ScriptBasic

Wed Jun 19, 2019 8:07 am

Heater wrote:
Wed Jun 19, 2019 5:11 am
Perhaps timeless, but still a new kid on the block. C is nearly 50 years old, Python 30 years...
Fortran is 65 years old.

User avatar
RichardRussell
Posts: 590
Joined: Thu Jun 21, 2012 10:48 am

Re: ScriptBasic

Wed Jun 19, 2019 8:14 am

ScriptBasic wrote:
Wed Jun 19, 2019 12:20 am
It's been around now for 20+ years.
BBC BASIC: 38 years (and I was there at the very start).

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Wed Jun 19, 2019 9:29 am

Perhaps call it ScriptBasicPlus. That acknowledges its legacy, identifies it as different, the latest, and allows "that's the old non-plus version" to be used when people discover the old stuff which isn't going away.

That also means "sb" as the executable name will make sense.

It also means "scriptbasic.org" and the "sb-dev" git retain some meaning and you can probably keep using those with a new "scriptbasicplus.org" site redirecting or linking to those.

There's plausibility that "ScriptBasic and "SB" now refers to ScriptBasicPlus" which means its recent use, and how it's going to get known in the future, isn't entirely wrong, isn't as confusing as it has been.

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Wed Jun 19, 2019 11:29 am

Back to raising errors. It appears that returning a number from the interface routines will cause an error to be raised. If that number is the value of a known error then the appropriate error message will be reported. Other values indicate an extension specific error. A value of 0x81001 does that for the MySQL extension. So -

Code: Select all

besFUNCTION(GPIO_readPin)
  long pin;
  besARGUMENTS("i")
    &pin
  besARGEND
  if (pin < 0)
  {
    return 0x81001; // Failed
  }
  besRETURN_LONG(readPin(pin))
besEND

That then needs a "besSUB_ERRMSG" routine included in interface.c which translates an error number into a meaningful string which describes the actual error message. So we'll start simple, just give a generic message no matter what the actual error -

Code: Select all

besSUB_ERRMSG
  return "Oops - Something went wrong";
besEND
And when we run that it partially works -

Code: Select all

(0): error 0x00081001:Extension specific error: %s
The error number is correct, is whatever is specified, but the conversion to text isn't being done.

It appears this is a bug in ScriptBasic, possibly simply not calling the besSUB_ERRMSG routine, because similar error messages are given when errors occur in the MySQL and cURL extensions, which are intended to include comprehensive error reporting -

https://www.scriptbasic.org/forum/index ... 24#msg1624
https://www.scriptbasic.org/forum/index ... ml#msg1633

As was noted in the first link, that was running a 2003 version 2.0 of ScriptBasic. It would seem this bug has been present for at least 15 years.

I also noted the reference to version 2.2 being the latest. As the Pi version is 2.1 does that mean we are not using the latest version ?

With Peter no longer interested in ScriptBasic; who fixes bugs like this ? Is there any official maintainer ( AIR ? ) - or is it a case of "if you can figure out how to fix a bug then please do so, otherwise it's just going to stay how it is, buggy forever" ?

User avatar
John_Spikowski
Posts: 1586
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 19, 2019 12:09 pm

With Peter no longer interested in ScriptBasic; who fixes bugs like this ? Is there any official maintainer ( AIR ? ) - or is it a case of "if you can figure out how to fix a bug then please do so, otherwise it's just going to stay how it is, buggy forever" ?
Peter hasn't touched the code in 15 years. AIR is the lead developer and has been involved with ScriptBasic almost as long as I have. ScriptBasic's best fit is with the RPi family. IMHO

AIR would be the best person to ask about how to deal with errors in extension modules.He spends more time under the SB covers then I do.

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Wed Jun 19, 2019 12:29 pm

ScriptBasic wrote:
Wed Jun 19, 2019 12:09 pm
With Peter no longer interested in ScriptBasic; who fixes bugs like this ? Is there any official maintainer ( AIR ? ) - or is it a case of "if you can figure out how to fix a bug then please do so, otherwise it's just going to stay how it is, buggy forever" ?
Peter hasn't touched the code in 15 years. AIR is the lead developer and has been involved with ScriptBasic almost as long as I have.
So is the answer that AIR will be investigating with the intent of fixing this bug ?

Or is it going to stay until I, or maybe someone else, decides to dive into the source code to investigate and fix it ?
ScriptBasic wrote:
Wed Jun 19, 2019 12:09 pm
ScriptBasic's best fit is with the RPi family. IMHO
Perhaps but utterly irrelevant. Promoting and evangelising ScriptBasic is fair enough but you don't have to use every opportunity to do that. I don't care what it's a best fit with, only whether it's usable for the things I might want to do with it.

What would really sell ScriptBasic is, "We found the bug, fixed it. Please run 'git update' and rebuild. Job done', even a 'We're investigating and will get it fixed'.
ScriptBasic wrote:
Wed Jun 19, 2019 12:09 pm
AIR would be the best person to ask about how to deal with errors in extension modules.He spends more time under the SB covers then I do.
I have figured out how to deal with errors. It is now getting that ScriptBasic bug investigated and fixed so it fully reports the errors I am raising which is the issue now.

User avatar
John_Spikowski
Posts: 1586
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 19, 2019 12:42 pm

Thanks Hippy!

The more informational you can provide about problems makes them easier to fix. Let's see what AIR has to say about this one. Not reporting the text behind the error code (in hex) sucks!

User avatar
John_Spikowski
Posts: 1586
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 19, 2019 1:42 pm

Perhaps but utterly irrelevant. Promoting and evangelising ScriptBasic is fair enough but you don't have to use every opportunity to do that. I don't care what it's a best fit with, only whether it's usable for the things I might want to do with it.
It has taken me almost two months to get three respected members of the forum to try ScriptBasic. Hopefully you guys while help carry the torch.

Heater
Posts: 13685
Joined: Tue Jul 17, 2012 3:02 pm

Re: ScriptBasic

Wed Jun 19, 2019 3:44 pm

Guys,

"Fortran: 65 years old.
BBC BASIC: 38 years"

I know. I was restricting myself to languages ScriptBasic, or anyone, is likely to have actually used.

ScriptBasic,
It has taken me almost two months to get three respected members of the forum to try ScriptBasic. Hopefully you guys while help carry the torch.
I'm not sure I'm included in the set of "respected members", many would argue otherwise I'm sure.

Whilst it's been fun to get ScriptBasic working, along with all the other Fibonacci Challenge contenders, I don't see that it has a place in my plans for programming anything in the future. That is no reflection on ScriptBasic necessarily but the reality is there are more than enough other languages out there, many of which are far more fitting for the things I'm likely to do.

I am curious to find out where all that memory goes in the hfibo.sb mind...
Last edited by Heater on Wed Jun 19, 2019 7:09 pm, edited 1 time in total.
Memory in C++ is a leaky abstraction .

jahboater
Posts: 4772
Joined: Wed Feb 04, 2015 6:38 pm

Re: ScriptBasic

Wed Jun 19, 2019 3:57 pm

Heater wrote:
Wed Jun 19, 2019 3:44 pm
"Fortran: 65 years old.
BBC BASIC: 38 years"

I know. I was restricting myself to languages ScriptBasic, or anyone, is likely to have actually used.
Fair enough. However I do think most programmers of our generation will have used Fortran. Long ago it was as popular as C is now.
Also Fortran 2018 looks like a highly capable modern language.

User avatar
John_Spikowski
Posts: 1586
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 19, 2019 4:38 pm

You, hippy and ejolson are my three respected members I referred to.

I understand you may not have a requirement for its use but helping out others that may would be appreciated.
I am curious to find out where all that memory goes in the hfibo.bs mind.
HFIBO is hungriest Fibo at the table

jalih
Posts: 94
Joined: Mon Apr 15, 2019 3:54 pm

Re: ScriptBasic

Wed Jun 19, 2019 6:17 pm

jahboater wrote:
Wed Jun 19, 2019 3:57 pm
Fair enough. However I do think most programmers of our generation will have used Fortran. Long ago it was as popular as C is now.
Also Fortran 2018 looks like a highly capable modern language.
I can program in Fortran (badly) but prefer using PL/I most of the time. How many of you have tried programming games in Fortran?

Below is a simple Minesweeper game written for Silverfrost FTN95:

Code: Select all

module sweeper
  use mswin  
  implicit none

  integer, parameter :: unselected = 1, selected = 2, marked = 4
  integer, parameter :: g_handle = 7, r_handle = 8
  integer, parameter :: brdsz = 10, tile_size = 32, num_tiles = 10, easy = 15, normal = 25, hard = 35
  integer, parameter :: brdszi = brdsz + 2, gr_size = tile_size * num_tiles

  character (len=9) :: tiles(0:8) = (/'n0','n1','n2','n3','n4','n5','n6','n7','n8'/)

  integer :: score, cleared, mines, difficulty = normal
  logical :: finished
    
  integer :: gr_handle

  type POINT
    integer :: x, y
  end type POINT

  type tile
    logical :: mine
    integer :: state
  end type tile

  type(tile) :: board(0:11,0:11)
    
  contains 

    integer function sc_func()
      integer :: i
      
      call [email protected]()
      i = [email protected](r_handle,gr_size,gr_size)
      call init_board()
      i = [email protected](g_handle,0,0,gr_size,gr_size,r_handle,0,0,gr_size,gr_size,SRCCOPY)
        
      sc_func=1
    end function sc_func


    integer function cc_func()
      integer :: i
      
      i = [email protected](r_handle)
        
      cc_func=-1
    end function cc_func


    integer function timer_func()
      integer :: i
      type(POINT) :: pt
        
      call GetCursorPos(pt)
      call ScreenToClient(gr_handle, pt)

      if (pt%x.lt.0.or.pt%x.gt.gr_size.or.pt%y.lt.0.or.pt%y.gt.gr_size) then
        i = [email protected](g_handle,0,0,gr_size,gr_size,r_handle,0,0,gr_size,gr_size,SRCCOPY)
      end if  
        
      timer_func=1
    end function timer_func


    integer function gr_func()
      integer :: mx,my
      integer :: bx, by
      integer :: i, j
      logical :: zeros
      real :: percentage
      character (len=20) :: reason

      if (finished) then
        gr_func = 2
        return
      end if

      zeros = .false.

      j = [email protected](r_handle)
        
      mx = [email protected]('graphics_mouse_x')
      my = [email protected]('graphics_mouse_y')

      bx = mx/tile_size+1
      by = my/tile_size+1

      reason = [email protected]('CALLBACK_REASON')

      if (reason.eq.'MOUSE_LEFT_CLICK') then
        i = board_check(by,bx)
        if (i.eq.-1 .and. board(by, bx)%state.ne.marked) then
          board(by, bx)%state = selected
          call display_mines()
          call display_square(by,bx, 'explosion')
          finished = .true.
        else if (i.ge.0 .and. i.le.8) then
          score = score + 1
          board(by, bx)%state = selected
          call display_square(by,bx,tiles(i))
          if (i.eq.0) then
            call display_zeros(by,bx)
            zeros = .true.
          end if
          percentage = real(score)/((brdsz*brdsz)-mines)*100
          cleared = int(percentage)
          if((score + mines).eq.(brdsz*brdsz)) then 
            call display_mines()
            finished = .true.
          end if
        end if
      else if (reason.eq.'MOUSE_RIGHT_CLICK'.and.board(by, bx)%state.ne.selected) then
        call mark_square(by, bx)
      endif
        
      j = [email protected](g_handle)
      j = [email protected](g_handle,0,0,gr_size,gr_size,r_handle,0,0,gr_size,gr_size,SRCCOPY)
      if(finished.and.(score + mines).ne.(brdsz*brdsz)) then
        call [email protected]
        j = [email protected]('explosionwave')
        gr_func=2
        return
      else if(zeros) then
        call [email protected]
        j = [email protected]('zeroswave')
        gr_func=2
        return
      end if

      select case(board(by, bx)%state)
        case(unselected)
          call display_square(by,bx,'tinted1')
        case(marked)
          call display_square(by,bx,'tinted2')
      end select

      gr_func=1
    end function gr_func


    integer function about_box_cb()   
      integer :: i
      
      [email protected]('%ca[About Minesweeper]&')
      [email protected]('%bg[BTNFACE]&')
      [email protected]('%ic[sweeper_icon] Minesweeper%ff&')
      [email protected]('%nlSimple game example using &')
      [email protected]('FTN95 and Clearwin+. %nl&')
      [email protected]('%ff%nl&')
      [email protected]('%taSend comments and insults to: %nl&')
      [email protected]('%ta    [email protected]%nl&')
      [email protected]('%ff%2nl&')
      [email protected]('%cn%`10tt[OK]')
      about_box_cb = 1   
    end function about_box_cb


    integer function menu_handler_cb()
      character (len=20) :: menuitem

      menuitem = [email protected]('current_menu_item')
      menu_handler_cb = 1

      select case(menuitem)
        case('&File~E&xit')
          menu_handler_cb = 0
        case('&File~&Reset')
          call init_board()
        case('&Help~&About')
          menu_handler_cb = about_box_cb()
        case('&Difficulty~&Easy')
          if(difficulty.ne.easy) then
            difficulty = easy
            call init_board()
            call [email protected](0,'[&File[&Reset,-se,E&xit],&Difficulty[-rc &Easy,&Normal, &Hard],&Help[&About]]')
          end if
        case('&Difficulty~&Normal')
          if(difficulty.ne.normal) then
            difficulty = normal
            call init_board()
            call [email protected](0,'[&File[&Reset,-se,E&xit],&Difficulty[&Easy,-rc &Normal, &Hard],&Help[&About]]')
          end if
        case('&Difficulty~&Hard')
          if(difficulty.ne.hard) then
            difficulty = hard
            call init_board()
            call [email protected](0,'[&File[&Reset,-se,E&xit],&Difficulty[&Easy,&Normal, -rc &Hard],&Help[&About]]')
          end if
      end select
      
    end function menu_handler_cb


    subroutine init_board()
      integer :: i, j, bx, by
      type(POINT) :: pt

      finished = .false.
      score = 0
      cleared = 0
      mines = 0
        
      do i = 0, brdszi - 1, 1
        do j = 0, brdszi - 1, 1
          board(i,j)%mine = .false.
          board(i,j)%state = unselected
        end do
      end do

      do i = 1, difficulty, 1
        j = int(([email protected]()*(brdsz*brdsz))-1)
        if (.not.board((j/brdsz)+1,mod(j,brdsz)+1)%mine) then
          board((j/brdsz)+1,mod(j,brdsz)+1)%mine = .true.
          mines = mines + 1
        end if
      end do

      call GetCursorPos(pt)
      call ScreenToClient(gr_handle, pt)

      i = [email protected](r_handle)
      call draw_board()
      i = [email protected](g_handle)
      i = [email protected](g_handle,0,0,gr_size,gr_size,r_handle,0,0,gr_size,gr_size,SRCCOPY)
      if (pt%x.ge.0.or.pt%x.le.gr_size.or.pt%y.ge.0.or.pt%y.le.gr_size) then
        bx = pt%x/tile_size+1
        by = pt%y/tile_size+1
        call display_square(by,bx,'tinted1')
      end if  

    end subroutine init_board
        

    subroutine display_square(i,j,img)
      integer :: i, j, k
      character (len=*) ::img

      k = [email protected](img, (j-1)*tile_size, (i-1)*tile_size)

    end subroutine display_square


    subroutine mark_square(i, j)
      integer :: i, j
        
      select case(board(i,j)%state)
        case(unselected)
          board(i,j)%state = marked
          call display_square(i,j,'flag')
        case(marked)
          board(i,j)%state = unselected
          call display_square(i,j,'tile')
      end select
    end subroutine mark_square

            
    subroutine draw_board()
      integer :: i, j

      do i=1,brdsz,1
        do j=1,brdsz,1
          call display_square(i,j,'tile')
        end do
      end do
    end subroutine draw_board


    subroutine display_mines()
      integer :: i, j

      do i=1,brdsz,1
        do j=1,brdsz,1
          if (board(i,j)%mine) then
            call display_square(i,j,'mine')
          end if
        end do
      end do
    end subroutine display_mines


    integer function board_check(i,j)
      integer :: i, j, row, col, c
        
      if (board(i, j)%mine) then
        board_check= -1
        return
      end if
      if (and(board(i, j)%state, or(selected,marked)).ne.0) then
        board_check = -2
        return
      end if

      c = 0
      do row = i-1, i+1, 1
        do col = j-1, j+1, 1
          if (board(row, col)%mine) then
            c = c + 1
          end if
        end do
      end do

      board_check = c

    end function board_check


    recursive subroutine display_zeros(i, j)
      integer :: i, j
      integer :: row, col, tile

      do row = i-1, i+1, 1
        do col = j-1, j+1, 1
          if (row.lt.1 .or. row.gt.brdsz .or. col.lt.1 .or. col.gt.brdsz) then
            cycle
          end if

          tile = board_check(row,col)
          if (tile.eq.0) then
            score = score + 1
            board(row,col)%state = selected
            call display_square(row, col, 'n0')
            call display_zeros(row, col)
            else if (tile.ge.1 .and. tile.le.8) then
            board(row,col)%state = selected
            call display_square(row, col, tiles(tile))
            score = score + 1
          end if
        end do
      end do
    end subroutine display_zeros
        

end module sweeper


winapp
program testi
  use sweeper
  implicit none

  integer :: i
  
  [email protected]('%ww[no_border,no_maxbox]&')
  [email protected]('%ca[Minesweeper]%sy[3d_depressed]&')
  [email protected]('%mi[sweeper_icon]&')
  [email protected]('%fn[Arial]%tc[blue]%ts&',1.5d0)
  [email protected]('%obcleared: %3`rd%%    score: %3`rd%cb%ff&', cleared, score)
  [email protected]('%`mn&','[&File[&Reset,-se,E&xit],&Difficulty[&Easy, -rc &Normal, &Hard],&Help[&About]]', menu_handler_cb)
  [email protected]('%sc&',sc_func)
  [email protected]('%cc&',cc_func)
  [email protected]('%`cu&',CURSOR_ARROW)
  [email protected]('%`^gr[full_mouse_input,rgb_colours]&',gr_size,gr_size,g_handle,gr_func)
  [email protected]('%dl&', 0.2D0, timer_func)
  [email protected]('%lc',gr_handle)
end program testi


resources
  sweeper_icon icon sweeper.ico
  tile image tile.jpg
  n0 image n0.jpg
  n1 image n1.jpg
  n2 image n2.jpg
  n3 image n3.jpg
  n4 image n4.jpg
  n5 image n5.jpg
  n6 image n6.jpg
  n7 image n7.jpg
  n8 image n8.jpg
  flag image flag.jpg
  mine image mine.jpg
  explosion image explosion.jpg
  tinted1 image tinted1.jpg
  tinted2 image tinted2.jpg
  explosionwave sound explosion.wav
  zeroswave sound zero.wav
As you can see, it's not too hard programming language to work with... I also have versions for Basic, PL/I and 8th. Most of my Minesweeper games use recursive cascade algorithm for revealing zero tiles but 8th version uses iterative version like the original MS version.

John, how about porting this for ScriptBasic? ;)

Heater
Posts: 13685
Joined: Tue Jul 17, 2012 3:02 pm

Re: ScriptBasic

Wed Jun 19, 2019 7:16 pm

ScriptBasic,
You, hippy and ejolson are my three respected members I referred to.
Why, thank you.
HFIBO is hungriest Fibo at the table
Perhaps, but only in the SB encarnation.

Also the iterative fibo in 1mildibo.sb leaks memory as well.

Out of a suggestion by ejolson I wrapped the 1milfibo program into a function and called it repeatedly. Like so:

Code: Select all

import gmp2.bas

function fibo(n)
    a = 1
    b = 0
    p = 0
    q = 1

    while n > 0
        if (n % 2) = 0 then
            psq   = gmp2::mul(p, p)
            qsq   = gmp2::mul(q, q)
            twopq = gmp2::mul(p, q)
            twopq = gmp2::mul_si(twopq, 2)
            p     = gmp2::add(psq, qsq)
            q     = gmp2::add(twopq, qsq)
            n = n / 2
        else
            bq    = gmp2::mul(b, q)
            aq    = gmp2::mul(a, q)
            ap    = gmp2::mul(a, p)
            bp    = gmp2::mul(b, p)
            a     = gmp2::add(bq, aq)
            a     = gmp2::add(a, ap)
            b     = gmp2::add(bp, aq)
            fibo  = b
            n = n - 1
        end if
    wend
end function

count = 0
while 1
    fibo(4784969)
    count = count + 1
    print count,"\n"
wend
As it runs I could see memory being consumed in top. I left it to run and came back to find:

Code: Select all

...
...
704
705
706
Segmentation fault (core dumped)
The GMP extension has a memory leak.
Memory in C++ is a leaky abstraction .

Heater
Posts: 13685
Joined: Tue Jul 17, 2012 3:02 pm

Re: ScriptBasic

Wed Jun 19, 2019 7:20 pm

jahboater,
I do think most programmers of our generation will have used Fortran. Long ago it was as popular as C is now. Also Fortran 2018 looks like a highly capable modern language.
True enough.

It's just that after an introduction to programming at tech school in 1973 my next language was ALGOL at uni two years later. Somehow I missed out on Fortran.

Hey, How come there is no Fortran million digit fibo contender?
Memory in C++ is a leaky abstraction .

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Wed Jun 19, 2019 7:36 pm

jalih wrote:
Wed Jun 19, 2019 6:17 pm
Below is a simple Minesweeper game written for Silverfrost FTN95:
And that's Fortran ? Doesn't look like any Fortran I remember.

I would have to guess it is descended from the Ratfor pre-processor; because I can't see anything which otherwise gives it a claim to a Fortran heritage.

jahboater
Posts: 4772
Joined: Wed Feb 04, 2015 6:38 pm

Re: ScriptBasic

Wed Jun 19, 2019 7:59 pm

hippy wrote:
Wed Jun 19, 2019 7:36 pm
jalih wrote:
Wed Jun 19, 2019 6:17 pm
Below is a simple Minesweeper game written for Silverfrost FTN95:
And that's Fortran ? Doesn't look like any Fortran I remember.
Like "Modern C++" we also have "Modern Fortran" ........
I would have to guess it is descended from the Ratfor pre-processor; because I can't see anything which otherwise gives it a claim to a Fortran heritage.
I thought Ratfor produced really old Fortran, perhaps Fortran IV or Fortran 66.

User avatar
John_Spikowski
Posts: 1586
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 19, 2019 8:13 pm

The GMP extension has a memory leak.
How about running that test in Python or C and let's see if it leaks.

Heater
Posts: 13685
Joined: Tue Jul 17, 2012 3:02 pm

Re: ScriptBasic

Wed Jun 19, 2019 8:53 pm

We already did that in C. The example C code I made for you for kick starting the GMP extension using decimal strings to hold the numbers does not leak memory.

See code and memory leak test results here: viewtopic.php?f=31&t=240287&start=525#p1481907

The problem is in the GMP extsion or ScriptBasic itself.
Memory in C++ is a leaky abstraction .

User avatar
John_Spikowski
Posts: 1586
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 19, 2019 9:05 pm

I ran your test with my version of the GMP extension module which over frees everything and it still leaks.

Thankfully the following code doesn't. Even after a million fibo(78) runs in a loop. Memory use stayed constant during the test. The only thing that might point to SB if strings were used instead of long & doubles.to test for leaks. That would be my next guess.

Code: Select all

function fibo(n)
    a = 1
    b = 0
    p = 0
    q = 1

    while n > 0
        if (n % 2) = 0 then
            psq   = p * p
            qsq   = q * q
            twopq = p * q
            twopq = twopq * 2
            p     = psq + qsq
            q     = twopq + qsq
            n = n / 2
        else
            bq    = b * q
            aq    = a * q
            ap    = a * p
            bp    = b * p
            a     = bq + aq
            a     = a + ap
            b     = bp + aq
            fibo  = b
            n = n - 1
        end if
    wend
end function

count = 0
while 1
    fibo(78)
    count = count + 1
    print count,"\n"
wend
Houston (AIR) we have a problem!

User avatar
John_Spikowski
Posts: 1586
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 19, 2019 9:46 pm

In an attempt to bring strings into the picture the following routine was solid as a rock with memory use even though it generated a 1 meg string with each call to the fibo function. It seems string use in SB isn't the cause. I even set tststr as LOCAL and it didn't seem to have any adverse affect.

Code: Select all

function fibo(n)
    a = 1
    b = 0
    p = 0
    q = 1
    tststr = string(1024,"0")

    while n > 0
        if (n % 2) = 0 then
            psq   = p * p
            qsq   = q * q
            twopq = p * q
            twopq = twopq * 2
            p     = psq + qsq
            q     = twopq + qsq
            n = n / 2
            tststr &= tststr
        else
            bq    = b * q
            aq    = a * q
            ap    = a * p
            bp    = b * p
            a     = bq + aq
            a     = a + ap
            b     = bp + aq
            fibo  = b
            n = n - 1
            tststr &= tststr
        end if
    wend
    print format("[%d] - SLen: %d\n", count, len(tststr))
end function

count = 0
while 1
    fibo(78)
    count = count + 1
wend


[30776] - SLen: 1048576

User avatar
scruss
Posts: 2574
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: ScriptBasic

Wed Jun 19, 2019 10:09 pm

Heater wrote:
Wed Jun 19, 2019 7:20 pm
Hey, How come there is no Fortran million digit fibo contender?
I started one, but got sidetracked with Pari/GP. There are a couple of bignum libraries for Fortran, but they're not very advanced.
hippy wrote:
Wed Jun 19, 2019 7:36 pm
And that's Fortran ? Doesn't look like any Fortran I remember.

I would have to guess it is descended from the Ratfor pre-processor; because I can't see anything which otherwise gives it a claim to a Fortran heritage.
I guess you missed the Fortran-90 changeover to free-form code. All fixed-format F-77 is also valid F-90, a shrewd move by the ISO/IEC standard 1539:1991 committee.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
John_Spikowski
Posts: 1586
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Wed Jun 19, 2019 10:29 pm

To further narrow down where the leaking is going on I introduced an extension module call to the GMP fibo() function. Memory was solid so it looks like the way we are calling the GMP functions isn't freeing something.

Silver lining at least it doesn't seem to be an issue with core ScriptBasic which would be a job to find and fix.

I wonder if dereferencing the passed args once they are assigned a GMP handle would help?

Code: Select all

DECLARE SUB fibo ALIAS "fibofunc" LIB "gmp"

count = 0
WHILE 1
 fibo(4784969)
 count += 1
 PRINT count,"\n"
WEND
Last edited by John_Spikowski on Thu Jun 20, 2019 1:04 am, edited 3 times in total.

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Wed Jun 19, 2019 10:37 pm

scruss wrote:
Wed Jun 19, 2019 10:09 pm
I guess you missed the Fortran-90 changeover to free-form code. All fixed-format F-77 is also valid F-90, a shrewd move by the ISO/IEC standard 1539:1991 committee.
Definitely. Had left Fortran behind at the end of the 80's and only suffered the tail-end of Cobol. Pascal was the new kid on the block then.

Airr
Posts: 21
Joined: Sun Jun 16, 2019 5:17 pm

Re: ScriptBasic

Thu Jun 20, 2019 1:47 am

Code: Select all

$ valgrind --leak-check=full --show-leak-kinds=all scriba newfib.sb 
==1997== Memcheck, a memory error detector
==1997== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1997== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==1997== Command: scriba newfib.sb
==1997== 
==1997== 
==1997== HEAP SUMMARY:
==1997==     in use at exit: 32 bytes in 1 blocks
==1997==   total heap usage: 21,612 allocs, 21,611 frees, 451,336,226 bytes allocated
==1997== 
==1997== 32 bytes in 1 blocks are still reachable in loss record 1 of 1
==1997==    at 0x4C2DBC5: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1997==    by 0x54DC60E: _dlerror_run (dlerror.c:141)
==1997==    by 0x54DBF81: [email protected]@GLIBC_2.2.5 (dlopen.c:87)
==1997==    by 0x11EFEC: dynlolib_LoadLibrary (in /home/riveraa/sb/bin/scriba)
==1997==    by 0x180A7B: modu_LoadModule (in /home/riveraa/sb/bin/scriba)
==1997==    by 0x180C91: modu_GetFunctionByName (in /home/riveraa/sb/bin/scriba)
==1997==    by 0x12C0AA: COMMAND_EXTERNAL (in /home/riveraa/sb/bin/scriba)
==1997==    by 0x168DAA: execute_Execute_r (in /home/riveraa/sb/bin/scriba)
==1997==    by 0x169BE3: execute_Evaluate (in /home/riveraa/sb/bin/scriba)
==1997==    by 0x1431F5: COMMAND_LET (in /home/riveraa/sb/bin/scriba)
==1997==    by 0x168DAA: execute_Execute_r (in /home/riveraa/sb/bin/scriba)
==1997==    by 0x127484: scriba_Run (in /home/riveraa/sb/bin/scriba)
==1997== 
==1997== LEAK SUMMARY:
==1997==    definitely lost: 0 bytes in 0 blocks
==1997==    indirectly lost: 0 bytes in 0 blocks
==1997==      possibly lost: 0 bytes in 0 blocks
==1997==    still reachable: 32 bytes in 1 blocks
==1997==         suppressed: 0 bytes in 0 blocks
==1997== 
==1997== For counts of detected and suppressed errors, rerun with: -v
==1997== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

I don't know if using 'top' is the correct way to check for leaks; mem usage fluctuates even when the system is idling. While this was running, used mem in 'top' went from roughly 1.2M to 1.35M and back down again several times.

System Used:
Gigabyte Brix i3-5100u @2.1Ghz
16GB Memory
1TB mSata SSD

Running fresh install of antiX-17.4.1 (I just received this unit today, popped in the memory and drive and installed the OS within the last hour).


AIR.

Return to “Other programming languages”