Modifying a seq after unmarshling sometimes triggers am Attempt to read from nil?. If I read the sequence without modifying no error happens.
Example
import marshal
import streams
import times
import tables
type AiredEpisodeState * = ref object
airedAt * : DateTime
tvShowId * : string
seasonNumber * : int
number * : int
title * : string
type ShowsWatchlistState * = ref object
aired * : seq[AiredEpisodeState]
type UiState * = ref object
shows: ShowsWatchlistState
# Helpers to marshal and unmarshal
proc load * ( state : var UiState, file : string ) =
var strm = newFileStream( file, fmRead )
strm.load( state )
strm.close()
proc store * ( state : UiState, file : string ) =
var strm = newFileStream( file, fmWrite )
strm.store( state )
strm.close()
# 1. We fill the state initially
var state : UiState = UiState( shows: ShowsWatchlistState( aired: @[] ) )
# VERY IMPORTANT: For some reason, small numbers (like 2 or 3) don't trigger the bug. Anything above 7 or 8 on my machine triggers though
for i in 0..30:
var episode = AiredEpisodeState( airedAt: now(), tvShowId: "1", seasonNumber: 1, number: 1, title: "string" )
state.shows.aired.add( episode )
# 2. Store it in a file with the marshal module, and then load it back up
store( state, "repro.dat" )
load( state, "repro.dat" )
# 3. VERY IMPORTANT: Without this line, for some reason, everything works fine
state.shows.aired[ 0 ] = AiredEpisodeState( airedAt: now(), tvShowId: "1", seasonNumber: 1, number: 1, title: "string" )
# 4. And formatting the airedAt date will now trigger the exception
for ep in state.shows.aired:
echo( $ep.seasonNumber & "x" & $ep.number & " (" & $ep.airedAt & ")" )
Current Output
C:\Users\PedroSilva\Documents\Projects\Nim\watchlist\src\repro.nim(52) repro
C:\Program Files\nim-1.0.0\lib\pure\times.nim(2427) $
C:\Program Files\nim-1.0.0\lib\pure\times.nim(2308) format
C:\Program Files\nim-1.0.0\lib\pure\times.nim(2290) format
C:\Program Files\nim-1.0.0\lib\pure\times.nim(2005) formatPattern
C:\Program Files\nim-1.0.0\lib\system\strmantle.nim(24) eqStrings
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Expected Output
1x1 (2019-10-10T12:26:01+01:00)
1x1 (2019-10-10T12:26:01+01:00)
1x1 (2019-10-10T12:26:01+01:00)
...
Possible Solution
Additional Information
$ nim -v
Nim Compiler Version 1.0.0 [Windows: amd64]
Compiled at 2019-09-23
Copyright (c) 2006-2019 by Andreas Rumpf
git hash: f7a8fc46c0012033917582eb740dc0343c093e35
active boot switches: -d:release
Modifying a
seqafter unmarshling sometimes triggers amAttempt to read from nil?. If I read the sequence without modifying no error happens.Example
Current Output
Expected Output
Possible Solution
Additional Information