Author Topic: Bug REDIM _PRESERVE losing data?  (Read 2580 times)

0 Members and 1 Guest are viewing this topic.

Offline blametroi

  • Newbie
  • Posts: 26
    • View Profile
Bug REDIM _PRESERVE losing data?
« on: January 25, 2021, 09:22:34 am »
I've a two dimensional string array that I redim _preserve and the contents of the second row are lost. Sample code attached.

Line 63 "REDIM _PRESERVE definingKeywords(i-1, 2) AS STRING" is the key. Comment this out, and the print of definingKeywords(i,2) in line 82 shows the data read in earlier in the program. Uncomment it, and there's nothing showing for definingKeywords(i,2). definingKeywords(i,1) is fine throughout.

I've tried to come  up with a test program that isn't pulled out of a current project, but of course the bug does not manifest in those attempts.

EDIT: This is with the 1.5 development build 2e309e8. Recent but not the latest.

Code: QB64: [Select]
  1. ' redim_error2.bas
  2. '==============================================================
  3. '
  4. '==============================================================
  5.  
  6. OPTION _EXPLICIT ' i prefer it this way
  7. OPTION BASE 1 ' god meant it to be this way
  8.  
  9. '==============================================================
  10. ' global definitions, declarations, and initialization
  11. '==============================================================
  12.  
  13. ON ERROR GOTO handleError
  14.  
  15. 'constants
  16. CONST START_SIZE = 500
  17. CONST INCREMENT_SIZE = 250
  18.  
  19. 'types
  20.  
  21. 'shared and work
  22. REDIM SHARED definingKeywords(START_SIZE, 2) AS STRING ' over allocate, will trim
  23. REDIM SHARED tagTypes(START_SIZE) AS STRING ' also over allocated
  24. DIM numDefiningKeywords AS INTEGER
  25. DIM numTagTypes AS INTEGER
  26. DIM pLabel AS INTEGER
  27. DIM pInclude AS INTEGER
  28. DIM pKeyword AS INTEGER
  29. DIM delimiters AS STRING
  30. DIM currentTagType AS STRING
  31. DIM currentKeyword AS STRING
  32.  
  33. '==============================================================
  34. ' mainline
  35. '==============================================================
  36. mainline:
  37.  
  38. '==============================================================
  39. ' load our definitions
  40. '==============================================================
  41. RESTORE dataTagTypes
  42. i = 1
  43.     READ currentTagType
  44.     IF currentTagType = "" THEN
  45.         REDIM _PRESERVE tagTypes(i - 1)
  46.         EXIT DO
  47.     END IF
  48.     tagTypes(i) = currentTagType
  49.     i = i + 1
  50. numTagTypes = i - 1
  51.  
  52. RESTORE dataDefiningKeywords
  53. i = 1
  54.     READ currentKeyword, currentTagType
  55.     IF currentKeyword = "" THEN
  56.         'todo: redim _preserve is losing the 2nd dimension? dup and report
  57.         'REDIM _PRESERVE definingKeywords(i - 1, 2) AS STRING
  58.         EXIT DO
  59.     END IF
  60.     definingKeywords(i, 1) = currentKeyword
  61.     definingKeywords(i, 2) = currentTagType
  62.     PRINT definingKeywords(i, 1), definingKeywords(i, 2)
  63.     IF currentKeyword = "<label>" THEN pLabel = i
  64.     IF currentKeyword = "'$INCLUDE:" THEN pInclude = i
  65.     i = i + 1
  66. numDefiningKeywords = i - 1
  67.  
  68. delimiters = " :;()-+*/\"
  69.  
  70. '===============================================================
  71. ' got data?
  72. '===============================================================
  73.  
  74. FOR i = 1 TO numDefiningKeywords
  75.     PRINT definingKeywords(i, 1); ":"; definingKeywords(i, 2)
  76.  
  77. '===============================================================
  78. ' close files, we're done
  79. '===============================================================
  80.  
  81.  
  82.  
  83. '==============================================================
  84. ' handle general runtime errors, can override. override
  85. ' handlers should be added after this one.
  86. '==============================================================
  87.  
  88. handleError:
  89.  
  90. PRINT "Error" + STR$(ERR) + " at line" + STR$(_ERRORLINE)
  91.  
  92.  
  93. '==============================================================
  94. ' data blocks. if you are using data, in qb64 it needs to be
  95. ' defined before any subs/functions, so group it all here. i
  96. ' wish we could put each block closer to its use.
  97. '==============================================================
  98.  
  99. dataBlocksHere:
  100. DATA -9999
  101.  
  102. '==============================================================
  103. ' qb64 keywords types.
  104. '==============================================================
  105.  
  106. dataTagTypes:
  107. DATA "constant"
  108. DATA "function"
  109. DATA "include"
  110. DATA "label"
  111. DATA "type"
  112. DATA "variable"
  113. DATA ""
  114.  
  115. '==============================================================
  116. ' qb64 keywords types.
  117. '
  118. ' these are mostly sorted but two word variants are put in
  119. ' front of a single word form, eg "dim shared" comes before
  120. ' "dim". this simplifies the search. the list is short enough
  121. ' that the search will be sequential.
  122. '
  123. ' format is keyword text in upper case, and what type of
  124. ' entity this defines.
  125. '==============================================================
  126.  
  127. dataDefiningKeywords:
  128. DATA "ALIAS ","type"
  129. DATA "COMMON SHARED ","variable"
  130. DATA "COMMON ","variable"
  131. DATA "CONST ","constant"
  132. DATA "DECLARE DYNAMIC LIBRARY ","type"
  133. DATA "DECLARE LIBRARY ","type"
  134. DATA "DIM SHARED ","variable"
  135. DATA "DIM ","variable"
  136. DATA "FUNCTION ","function"
  137. DATA "<label>","label": ' there is no actual keyword text, rather colon
  138. DATA "REDIM _PRESERVE ","variable"
  139. DATA "REDIM SHARED ","variable"
  140. DATA "REDIM ","variable"
  141. DATA "SHARED ","variable"
  142. DATA "STATIC ","variable"
  143. DATA "SUB ","function"
  144. DATA "TYPE ","type"
  145. DATA "'$INCLUDE:","include"
  146. DATA "",""
  147.  
« Last Edit: January 25, 2021, 09:24:18 am by blametroi »

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Bug REDIM _PRESERVE losing data?
« Reply #1 on: January 25, 2021, 09:31:10 am »
You're probably hitting this long-standing bug: https://github.com/QB64Team/qb64/issues/3 (ignore the most recent comment).

Flip your dimensions so you only ever resize the last dimension and see if that behaves more predictably.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Bug REDIM _PRESERVE losing data?
« Reply #2 on: January 25, 2021, 09:38:10 am »
If you’re curious about what’s going on, watch Tutorial #4 here: https://www.qb64.org/forum/index.php?topic=1731.0

It’s probably more than you ever wanted to know about how information is stored in memory, and the whole video runs about 90 minutes, or so, so grab some popcorn and a soda before watching the whole thing.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline blametroi

  • Newbie
  • Posts: 26
    • View Profile
Re: Bug REDIM _PRESERVE losing data?
« Reply #3 on: January 25, 2021, 11:11:44 am »
Thanks guys, I do love a good Heisenbug. I've got a couple of ideas how to get around this, but wanted to report it.

Regarding "more than you ever wanted to know" ... in professional life I was an assembly language programmer writing systems software. I don't go that deep anymore, but I'd like to think I can if I need to :)

I'll come up with a workaround I trust. Thanks again.