tmarshalsegfault.nim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # issue #12405
  2. import std/[marshal, streams, times, tables, os, assertions]
  3. type AiredEpisodeState * = ref object
  4. airedAt * : DateTime
  5. tvShowId * : string
  6. seasonNumber * : int
  7. number * : int
  8. title * : string
  9. type ShowsWatchlistState * = ref object
  10. aired * : seq[AiredEpisodeState]
  11. type UiState * = ref object
  12. shows: ShowsWatchlistState
  13. # Helpers to marshal and unmarshal
  14. proc load * ( state : var UiState, file : string ) =
  15. var strm = newFileStream( file, fmRead )
  16. strm.load( state )
  17. strm.close()
  18. proc store * ( state : UiState, file : string ) =
  19. var strm = newFileStream( file, fmWrite )
  20. strm.store( state )
  21. strm.close()
  22. # 1. We fill the state initially
  23. var state : UiState = UiState( shows: ShowsWatchlistState( aired: @[] ) )
  24. # 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
  25. for i in 0..30:
  26. var episode = AiredEpisodeState( airedAt: now(), tvShowId: "1", seasonNumber: 1, number: 1, title: "string" )
  27. state.shows.aired.add( episode )
  28. # 2. Store it in a file with the marshal module, and then load it back up
  29. store( state, "tmarshalsegfault_data" )
  30. load( state, "tmarshalsegfault_data" )
  31. removeFile("tmarshalsegfault_data")
  32. # 3. VERY IMPORTANT: Without this line, for some reason, everything works fine
  33. state.shows.aired[ 0 ] = AiredEpisodeState( airedAt: now(), tvShowId: "1", seasonNumber: 1, number: 1, title: "string" )
  34. # 4. And formatting the airedAt date will now trigger the exception
  35. for ep in state.shows.aired:
  36. let x = $ep.seasonNumber & "x" & $ep.number & " (" & $ep.airedAt & ")"
  37. let y = $ep.seasonNumber & "x" & $ep.number & " (" & $ep.airedAt & ")"
  38. doAssert x == y