Hi! The following snippet produces an "attempt to access a nil address" error,
but only with the devel version, and only when **parseAtlas** is executed at
compile time (the offending line is marked with a comment; only **parseAtlas**
seems directly relevant):
const file = """
sprites.png
size: 1024,1024
format: RGBA8888
filter: Linear,Linear
repeat: none
char/slide_down
rotate: false
xy: 730, 810
size: 204, 116
orig: 204, 116
offset: 0, 0
index: -1
"""
type
AtlasPage = object
width, height: int
path: string
CtsStream = object
data: string
pos: int
# ************
# Crappy string stream implementation, because Nim's doesn't work at
compile time:
# Probably not interesting, but seems necessary to reproduce the bug.
# ************
proc atEnd(stream: CtsStream): bool =
stream.pos >= stream.data.len
proc readChar(stream: var CtsStream): char =
if stream.atEnd:
result = '\0'
else:
result = stream.data[stream.pos]
inc stream.pos
proc readLine(s: var CtsStream, line: var string): bool =
# This is pretty much copied from the standard library:
line.setLen(0)
while true:
var c = readChar(s)
if c == '\c':
c = readChar(s)
break
elif c == '\L': break
elif c == '\0':
if line.len > 0: break
else: return false
line.add(c)
result = true
proc peekLine(s: var CtsStream, line: var string): bool =
let oldPos = s.pos
result = s.readLine(line)
s.pos = oldPos
proc initCtsStream(data: string): CtsStream =
CtsStream(
pos: 0,
data: data
)
# ********************
# Interesting stuff happens here:
# ********************
proc parseAtlas(stream: var CtsStream) =
var pages: seq[AtlasPage] = @[]
while not stream.atEnd:
# Read a page:
pages.add(AtlasPage())
var page = addr pages[^1]
var line = ""
discard stream.readLine(line)
while peekLine(stream, line) and line != "":
echo page == nil
discard stream.readLine(line)
let u0 = 100 / page.width # <----------------------- Crash happens
here! --------
static:
var stream = initCtsStream(file)
parseAtlas(stream)
echo "Done!"
I couldn't shorten it any further. Even replacing the inner loop inside
**parseAtlas** with a for that has the same body and executes the same number
of times makes the error disappear. Perhaps I 'm doing something questionable
with that page pointer?