khchen / wNim

Nim's Windows GUI Framework
MIT License
326 stars 17 forks source link

Runtime error: modify seq while iterate, nim 0.20 (UPD) #22

Closed zolern closed 5 years ago

zolern commented 5 years ago

Runtime error occurs because new rule about seq's modification white iterate through it

For example: wNim/private/wApp.nim, proc wAppWindowDelete, line 73

I tested workaround, index for win to delete is saved and win is deleted from seq outside of iterator.

Before:

proc wAppWindowDelete(win: wWindow) =
  wTheApp.mWindowTable.del(win.mHwnd)

  for index, w in wTheApp.mTopLevelWindowList:
    if w == win:
      wTheApp.mTopLevelWindowList.del(index)

Now (updated):

proc wAppWindowDelete(win: wWindow) =
  wTheApp.mWindowTable.del(win.mHwnd)

  var indexesToDelete: seq[int]

  for index, w in wTheApp.mTopLevelWindowList:
    if w == win:
      indexesToDelete.add(index)

  while(indexesToDelete.len > 0):
    # delete from wTheApp.mTopLevelWindowList in reverse order to keep indexes intact
    wTheApp.mTopLevelWindowList.del(indexesToDelete.pop())

P.S. I worry that there may be other places with the same problem

khchen commented 5 years ago

Fixed.