Author Topic: memory leak and question  (Read 5987 times)

0 Members and 1 Guest are viewing this topic.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: memory leak and question
« Reply #15 on: September 29, 2020, 02:47:14 pm »
Hello

That is ok i figured as much i had to recheck that spot to make sure it was ok being in side the if statement. I understate how they work just not use to using them i have done so visual stuff in vb and vc but quick basic is my choice. I am so glade it has been ported to the 64 bit platform. I just need to see how what you are talking about works.

Badger

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: memory leak and question
« Reply #16 on: September 29, 2020, 02:55:40 pm »
Hello

i posted i thought i dont think i hit post ugg.

NO problems on the mistake it made me look at that spot to make sure that return should be in that if statement.

I know how subs and functions work i do a little work in vb and vc but my first love is qb 4.5. I am really glad you all ported qb to 64 bit.

badger

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: memory leak and question
« Reply #17 on: September 29, 2020, 04:24:56 pm »
A very quick demo of a customer input routine:

Code: QB64: [Select]
  1.  
  2.  
  3. GetRecord
  4. PRINT "This is the record you entered:"
  5. PRINT "Customer Name: "; n(1)
  6. PRINT "Customer Age :"; n(2)
  7. PRINT "Customer Sex :"; n(3)
  8.  
  9.  
  10.  
  11.  
  12.  
  13. SUB GetRecord
  14.     'A simple three field record:  name, age, sex
  15.     'I could use a TYPE with these, but there's not much point in that.  At least not for just a simple demo
  16.     'Instead, I'm just going to use a simple 3 element array, which I dimmed and shared in the main module
  17.     DIM c(3) AS STRING * 1 ' And this is just a simple array to hold a cursor so we know where we're entering data at
  18.     EntryOn = 1 'Start with the first field by default
  19.  
  20.     DO
  21.         IF EntryOn < 1 THEN EntryOn = 3 'same basic error checking to loop around our fields
  22.         IF EntryOn > 3 THEN EntryOn = 1 'same as above
  23.         CLS
  24.         FOR i = 1 TO 3: c(i) = " ": NEXT 'clear all cursors
  25.         c(EntryOn) = CHR$(219)
  26.         PRINT "Customer Name: "; n(1); c(1)
  27.         PRINT "Customer Age : "; n(2); c(2)
  28.         PRINT "Customer Sex : "; n(3); c(3)
  29.         PRINT
  30.         PRINT "<CTRL-ENTER> to finish."
  31.  
  32.         k = _KEYHIT
  33.         SELECT CASE k
  34.             CASE 18432 'up arrow
  35.                 EntryOn = EntryOn - 1
  36.             CASE 20480, 9 'down arrow, TAB
  37.                 EntryOn = EntryOn + 1
  38.             CASE 8 'backspace
  39.                 n(EntryOn) = LEFT$(n(EntryOn), LEN(n(EntryOn)) - 1)
  40.             CASE 65 TO 90, 97 TO 122, 32, 48 TO 57 'A to Z, a TO z, SPACE, 0 to 9
  41.                 n(EntryOn) = n(EntryOn) + CHR$(k)
  42.             CASE 13
  43.                 IF _KEYDOWN(100306) OR _KEYDOWN(100305) THEN 'CTRL is down when we hit enter
  44.                     EXIT SUB
  45.                 ELSE 'CTRL isn't down.  It's just an ENTER by itself.  Move to the next line
  46.                     EntryOn = EntryOn + 1
  47.                 END IF
  48.         END SELECT
  49.         _LIMIT 30
  50.     LOOP
  51.  

One short SUB, which is completely self contained in this case, does all the work for us, and it makes it quite easy to navigate and work with our fields.  I'm thinking this is the style input routine which bplus was talking about above, for use.  :)






https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: memory leak and question
« Reply #18 on: September 29, 2020, 05:30:22 pm »
Yeah I just finished my 50 liner, I turned it into a FUNCTION in case user wants to cancel or quit an edit so it would return 0, otherwise an accepted edit returns -1

Code: QB64: [Select]
  1. TYPE customer
  2.     id AS LONG
  3.     first AS STRING * 25
  4.     last AS STRING * 25
  5.     address AS STRING * 30
  6.     city AS STRING * 25
  7.     state AS STRING * 2
  8.     zip AS STRING * 5
  9.     phone AS STRING * 10
  10.  
  11. DIM SHARED nRecs ' for when you get a file going
  12.  
  13. DIM myRec AS customer 'edit this record over and over for testing
  14.     edit = EditCustomer(myRec, 1) ' assign to first record 1 if not Cancelled
  15.     CLS
  16.     IF edit THEN
  17.         PRINT "Record #:"; myRec.id
  18.         PRINT "   First:"; myRec.first
  19.         PRINT "    Last:"; myRec.last
  20.         PRINT " Address:"; myRec.address
  21.         PRINT "    City:"; myRec.city
  22.         PRINT "   State:"; myRec.state
  23.         PRINT "   Phone:"; myRec.phone
  24.     ELSE
  25.         PRINT "user cancelled edit"
  26.     END IF
  27.     PRINT "(SLEEPing) press any to continue..."
  28.     SLEEP
  29.     CLS
  30. 'if edit then fileRec myRec, 1
  31.  
  32. 'return 0 if user cancelled, -1 if record edit was accepted
  33. FUNCTION EditCustomer% (customerRec AS customer, recNumber AS LONG) ' use for new Customer or edit Customer
  34.     DIM rec(1 TO 7, 0 TO 1) AS STRING
  35.     rec(1, 0) = "First Name"
  36.     rec(2, 0) = "Last Name"
  37.     rec(3, 0) = "Address"
  38.     rec(4, 0) = "City"
  39.     rec(5, 0) = "State (abbrev:2)"
  40.     rec(6, 0) = "Zip (5)"
  41.     rec(7, 0) = "Phone" ' might split to Home and Mobile
  42.     IF customerRec.first <> "" THEN rec(1, 1) = customerRec.first
  43.     IF customerRec.last <> "" THEN rec(2, 1) = customerRec.last
  44.     IF customerRec.address <> "" THEN rec(3, 1) = customerRec.address
  45.     IF customerRec.city <> "" THEN rec(4, 1) = customerRec.city
  46.     IF customerRec.state <> "" THEN rec(5, 1) = customerRec.state
  47.     IF customerRec.zip <> "" THEN rec(6, 1) = customerRec.zip
  48.     IF customerRec.phone <> "" THEN rec(7, 1) = customerRec.phone
  49.     doAgain:
  50.     CLS
  51.     FOR i = 1 TO 7 'show what we have
  52.         PRINT i, rec(i, 0); " = "; rec(i, 1)
  53.     NEXT
  54.     PRINT: PRINT "Press a to Accept, n for New, a digit to edit field, or escape to Cancel."
  55.     choice$ = getChar$("aAnN1234567" + CHR$(27))
  56.     IF ASC(choice$) = 27 THEN ' maybe this should be a function
  57.         EditCustomer% = 0: EXIT FUNCTION 'cancelled  EDIT to 0
  58.     ELSEIF UCASE$(choice$) = "A" THEN
  59.         customerRec.id = recNumber 'assign the record
  60.         customerRec.first = rec(1, 1)
  61.         customerRec.last = rec(2, 1)
  62.         customerRec.address = rec(3, 1)
  63.         customerRec.city = rec(4, 1)
  64.         customerRec.state = rec(5, 1)
  65.         customerRec.zip = rec(6, 1)
  66.         customerRec.phone = rec(7, 1)
  67.         EditCustomer% = -1: EXIT FUNCTION 'done
  68.     ELSEIF UCASE$(choice$) = "N" THEN
  69.         CLS
  70.         FOR i = 1 TO 7
  71.             PRINT rec(i, 0); " > ";
  72.             INPUT ""; rec(i, 1)
  73.         NEXT
  74.         GOTO doAgain
  75.     ELSEIF INSTR("1234567", choice$) THEN
  76.         CLS
  77.         PRINT rec(VAL(choice$), 0); " > ";
  78.         INPUT ""; rec(VAL(choice$), 1)
  79.         GOTO doAgain
  80.     END IF
  81.  
  82. FUNCTION getChar$ (fromStr$) ' get a char$ usually for a menu
  83.     DIM OK AS INTEGER, k$
  84.     WHILE OK = 0
  85.         k$ = INKEY$
  86.         IF LEN(k$) THEN
  87.             IF INSTR(fromStr$, k$) <> 0 THEN OK = -1
  88.         END IF
  89.         _LIMIT 200
  90.     WEND
  91.     _KEYCLEAR
  92.     getChar$ = k$
  93.  
  94.  

EDIT: a last minute change set the cancel to -1, supposed to be 0, fixed.
Updated the record description returned to main code.
Edit2: Got first and last name switched when showing record returned from Function, fixed.

Typically blundering my way to better code ;-))
« Last Edit: September 29, 2020, 08:41:55 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: memory leak and question
« Reply #19 on: September 30, 2020, 05:52:09 am »
so we can say from a large main with so much GOSUB/RETURN (in the place of GOTO labels) we get a shrink SUB of Steve!

just to let be more readable for my eyes that code I have toggled few things
Code: QB64: [Select]
  1. CONST cName = 1, cAge = 2, cSex = 3
  2.  
  3.  
  4. GetRecord
  5. PRINT "This is the record you entered:"
  6. PRINT "Customer Name: "; n(cName)
  7. PRINT "Customer Age :"; n(cAge)
  8. PRINT "Customer Sex :"; n(cSex)
  9.  
  10.  
  11.  
  12.  
  13.  
  14. SUB GetRecord
  15.     'A simple three field record:  name, age, sex
  16.     'I could use a TYPE with these, but there's not much point in that.  At least not for just a simple demo
  17.     'Instead, I'm just going to use a simple 3 element array, which I dimmed and shared in the main module
  18.     DIM c(3) AS STRING * 1 ' And this is just a simple array to hold a cursor so we know where we're entering data at
  19.     EntryOn = 1 'Start with the first field by default
  20.  
  21.     DO
  22.         IF EntryOn < cName THEN EntryOn = cSex 'same basic error checking to loop around our fields
  23.         IF EntryOn > cSex THEN EntryOn = cName 'same as above
  24.         CLS
  25.         FOR i = 1 TO 3: c(i) = " ": NEXT 'clear all cursors
  26.         c(EntryOn) = CHR$(219)
  27.         PRINT "Customer Name: "; n(cName); c(cName)
  28.         PRINT "Customer Age : "; n(cAge); c(cAge)
  29.         PRINT "Customer Sex : "; n(cSex); c(cSex)
  30.         PRINT
  31.         PRINT "<CTRL-ENTER> to finish."
  32.  
  33.         k = _KEYHIT
  34.         SELECT CASE k
  35.             CASE 18432 'up arrow
  36.                 EntryOn = EntryOn - 1
  37.             CASE 20480, 9 'down arrow, TAB
  38.                 EntryOn = EntryOn + 1
  39.             CASE 8 'backspace
  40.                 n(EntryOn) = LEFT$(n(EntryOn), LEN(n(EntryOn)) - 1)
  41.             CASE 65 TO 90, 97 TO 122, 32, 48 TO 57 'A to Z, a TO z, SPACE, 0 to 9
  42.                 n(EntryOn) = n(EntryOn) + CHR$(k)
  43.             CASE 13
  44.                 IF _KEYDOWN(100306) OR _KEYDOWN(100305) THEN 'CTRL is down when we hit enter
  45.                     EXIT SUB
  46.                 ELSE 'CTRL isn't down.  It's just an ENTER by itself.  Move to the next line
  47.                     EntryOn = EntryOn + 1
  48.                 END IF
  49.         END SELECT
  50.         _LIMIT 30
  51.     LOOP
  52.  
  53.  
  54.  
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: memory leak and question
« Reply #20 on: September 30, 2020, 05:53:34 am »
So why I must waste only Steve's work, it is also your turn:
in a gray office program it is beautiful to see more colors...
TA-DA!
Code: QB64: [Select]
  1. TYPE customer
  2.     id AS LONG
  3.     first AS STRING * 25
  4.     last AS STRING * 25
  5.     address AS STRING * 30
  6.     city AS STRING * 25
  7.     state AS STRING * 2
  8.     zip AS STRING * 5
  9.     phone AS STRING * 10
  10.  
  11. DIM SHARED nRecs ' for when you get a file going
  12.  
  13. DIM myRec AS customer 'edit this record over and over for testing
  14.     edit = EditCustomer(myRec, 1) ' assign to first record 1 if not Cancelled
  15.     CLS
  16.     IF edit THEN
  17.         PRINT "Record #:"; myRec.id
  18.         PRINT "   First:"; myRec.first
  19.         PRINT "    Last:"; myRec.last
  20.         PRINT " Address:"; myRec.address
  21.         PRINT "    City:"; myRec.city
  22.         PRINT "   State:"; myRec.state
  23.         PRINT "   Phone:"; myRec.phone
  24.     ELSE
  25.         PRINT "user cancelled edit"
  26.     END IF
  27.     PRINT "(SLEEPing) press any to continue..."
  28.     SLEEP
  29.     CLS
  30. 'if edit then fileRec myRec, 1
  31.  
  32. 'return 0 if user cancelled, -1 if record edit was accepted
  33. FUNCTION EditCustomer% (customerRec AS customer, recNumber AS LONG) ' use for new Customer or edit Customer
  34.     DIM rec(1 TO 7, 0 TO 1) AS STRING
  35.     rec(1, 0) = "First Name"
  36.     rec(2, 0) = "Last Name"
  37.     rec(3, 0) = "Address"
  38.     rec(4, 0) = "City"
  39.     rec(5, 0) = "State (abbrev:2)"
  40.     rec(6, 0) = "Zip (5)"
  41.     rec(7, 0) = "Phone" ' might split to Home and Mobile
  42.     IF customerRec.first <> "" THEN rec(1, 1) = customerRec.first
  43.     IF customerRec.last <> "" THEN rec(2, 1) = customerRec.last
  44.     IF customerRec.address <> "" THEN rec(3, 1) = customerRec.address
  45.     IF customerRec.city <> "" THEN rec(4, 1) = customerRec.city
  46.     IF customerRec.state <> "" THEN rec(5, 1) = customerRec.state
  47.     IF customerRec.zip <> "" THEN rec(6, 1) = customerRec.zip
  48.     IF customerRec.phone <> "" THEN rec(7, 1) = customerRec.phone
  49.     doAgain:
  50.     CLS
  51.     FOR i = 1 TO 7 'show what we have
  52.         COLOR i: PRINT i, rec(i, 0); " = "; rec(i, 1)
  53.     NEXT
  54.  
  55.     PRINT: PRINT "Press ";: COLOR 2: PRINT "a";: COLOR 7: PRINT " to Accept, ";
  56.     COLOR 3: PRINT "n";: COLOR 7: PRINT " for New, a ";
  57.     COLOR 4: PRINT "digit";: COLOR 7: PRINT " to edit field, or ";
  58.     COLOR 5: PRINT "escape";: COLOR 7: PRINT " to Cancel."
  59.     choice$ = getChar$("aAnN1234567" + CHR$(27))
  60.     IF ASC(choice$) = 27 THEN ' maybe this should be a function
  61.         EditCustomer% = 0: EXIT FUNCTION 'cancelled  EDIT to 0
  62.     ELSEIF UCASE$(choice$) = "A" THEN
  63.         customerRec.id = recNumber 'assign the record
  64.         customerRec.first = rec(1, 1)
  65.         customerRec.last = rec(2, 1)
  66.         customerRec.address = rec(3, 1)
  67.         customerRec.city = rec(4, 1)
  68.         customerRec.state = rec(5, 1)
  69.         customerRec.zip = rec(6, 1)
  70.         customerRec.phone = rec(7, 1)
  71.         EditCustomer% = -1: EXIT FUNCTION 'done
  72.     ELSEIF UCASE$(choice$) = "N" THEN
  73.         CLS
  74.         FOR i = 1 TO 7
  75.             PRINT rec(i, 0); " > ";
  76.             INPUT ""; rec(i, 1)
  77.         NEXT
  78.         GOTO doAgain
  79.     ELSEIF INSTR("1234567", choice$) THEN
  80.         CLS
  81.         PRINT rec(VAL(choice$), 0); " > ";
  82.         INPUT ""; rec(VAL(choice$), 1)
  83.         GOTO doAgain
  84.     END IF
  85.  
  86. FUNCTION getChar$ (fromStr$) ' get a char$ usually for a menu
  87.     DIM OK AS INTEGER, k$
  88.     WHILE OK = 0
  89.         k$ = INKEY$
  90.         IF LEN(k$) THEN
  91.             IF INSTR(fromStr$, k$) <> 0 THEN OK = -1
  92.         END IF
  93.         _LIMIT 200
  94.     WEND
  95.     _KEYCLEAR
  96.     getChar$ = k$
  97.  
  98.  
  99.  
« Last Edit: September 30, 2020, 05:54:43 am by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: memory leak and question
« Reply #21 on: September 30, 2020, 06:04:51 am »
just to follow my friend enemy of GOTO
here an alternative way of GOTO into FUNCTION
Code: QB64: [Select]
  1. TYPE customer
  2.     id AS LONG
  3.     first AS STRING * 25
  4.     last AS STRING * 25
  5.     address AS STRING * 30
  6.     city AS STRING * 25
  7.     state AS STRING * 2
  8.     zip AS STRING * 5
  9.     phone AS STRING * 10
  10.  
  11. DIM SHARED nRecs ' for when you get a file going
  12.  
  13. DIM myRec AS customer 'edit this record over and over for testing
  14.     edit = EditCustomer(myRec, 1) ' assign to first record 1 if not Cancelled
  15.     CLS
  16.     IF edit THEN
  17.         PRINT "Record #:"; myRec.id
  18.         PRINT "   First:"; myRec.first
  19.         PRINT "    Last:"; myRec.last
  20.         PRINT " Address:"; myRec.address
  21.         PRINT "    City:"; myRec.city
  22.         PRINT "   State:"; myRec.state
  23.         PRINT "   Phone:"; myRec.phone
  24.     ELSE
  25.         PRINT "user cancelled edit"
  26.     END IF
  27.     PRINT "(SLEEPing) press any to continue..."
  28.     SLEEP
  29.     CLS
  30. 'if edit then fileRec myRec, 1
  31.  
  32. 'return 0 if user cancelled, -1 if record edit was accepted
  33. FUNCTION EditCustomer% (customerRec AS customer, recNumber AS LONG) ' use for new Customer or edit Customer
  34.     DIM rec(1 TO 7, 0 TO 1) AS STRING
  35.     rec(1, 0) = "First Name"
  36.     rec(2, 0) = "Last Name"
  37.     rec(3, 0) = "Address"
  38.     rec(4, 0) = "City"
  39.     rec(5, 0) = "State (abbrev:2)"
  40.     rec(6, 0) = "Zip (5)"
  41.     rec(7, 0) = "Phone" ' might split to Home and Mobile
  42.     IF customerRec.first <> "" THEN rec(1, 1) = customerRec.first
  43.     IF customerRec.last <> "" THEN rec(2, 1) = customerRec.last
  44.     IF customerRec.address <> "" THEN rec(3, 1) = customerRec.address
  45.     IF customerRec.city <> "" THEN rec(4, 1) = customerRec.city
  46.     IF customerRec.state <> "" THEN rec(5, 1) = customerRec.state
  47.     IF customerRec.zip <> "" THEN rec(6, 1) = customerRec.zip
  48.     IF customerRec.phone <> "" THEN rec(7, 1) = customerRec.phone
  49.     doAgain = -1
  50.     DO WHILE doAgain
  51.         CLS
  52.         FOR i = 1 TO 7 'show what we have
  53.             COLOR i: PRINT i, rec(i, 0); " = "; rec(i, 1)
  54.         NEXT
  55.  
  56.         PRINT: PRINT "Press ";: COLOR 2: PRINT "a";: COLOR 7: PRINT " to Accept, ";
  57.         COLOR 3: PRINT "n";: COLOR 7: PRINT " for New, a ";
  58.         COLOR 4: PRINT "digit";: COLOR 7: PRINT " to edit field, or ";
  59.         COLOR 5: PRINT "escape";: COLOR 7: PRINT " to Cancel."
  60.         choice$ = getChar$("aAnN1234567" + CHR$(27))
  61.         IF ASC(choice$) = 27 THEN ' maybe this should be a function
  62.             EditCustomer% = 0: EXIT FUNCTION 'cancelled  EDIT to 0
  63.         ELSEIF UCASE$(choice$) = "A" THEN
  64.             customerRec.id = recNumber 'assign the record
  65.             customerRec.first = rec(1, 1)
  66.             customerRec.last = rec(2, 1)
  67.             customerRec.address = rec(3, 1)
  68.             customerRec.city = rec(4, 1)
  69.             customerRec.state = rec(5, 1)
  70.             customerRec.zip = rec(6, 1)
  71.             customerRec.phone = rec(7, 1)
  72.             doAgain = 0
  73.             EditCustomer% = -1: EXIT FUNCTION 'done
  74.         ELSEIF UCASE$(choice$) = "N" THEN
  75.             CLS
  76.             FOR i = 1 TO 7
  77.                 PRINT rec(i, 0); " > ";
  78.                 INPUT ""; rec(i, 1)
  79.             NEXT
  80.             doAgain = -1
  81.         ELSEIF INSTR("1234567", choice$) THEN
  82.             CLS
  83.             PRINT rec(VAL(choice$), 0); " > ";
  84.             INPUT ""; rec(VAL(choice$), 1)
  85.             doAgain = -1
  86.         END IF
  87.     LOOP
  88.  
  89. FUNCTION getChar$ (fromStr$) ' get a char$ usually for a menu
  90.     DIM OK AS INTEGER, k$
  91.     WHILE OK = 0
  92.         k$ = INKEY$
  93.         IF LEN(k$) THEN
  94.             IF INSTR(fromStr$, k$) <> 0 THEN OK = -1
  95.         END IF
  96.         _LIMIT 200
  97.     WEND
  98.     _KEYCLEAR
  99.     getChar$ = k$
  100.  
Programming isn't difficult, only it's  consuming time and coffee