I've been chasing some random crashes in a program I'm writing. I was able to
reduce the problem to the small program below.
type
Vector = ref array[2, float64]
Matrix = ref array[2, Vector]
proc newVector(): Vector =
new(result)
proc newMatrix(): Matrix =
new(result)
for ix in 0 .. 1:
result[ix] = newVector()
let m = newMatrix()
m[0][0] = 1.0
echo "m[0][0] = ", m[0][0]
GC_fullCollect()
m[0][0] = 2.0
echo "m[0][0] = ", m[0][0]
This program crashes during the garbage collection with a
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
It looks like the vectors in the **m** matrix are being garbage collected at
the first collection. This obviously creates problems.
Replacing
for ix in 0 .. 1:
result[ix] = newVector()
in **newMatrix** with
for v in result[].mitems:
v = newVector()
does work.
I'm not very sure what is going on. I think that the original version should
work. Am I wrong in thinking that ? Or did I make a mistake ? Or is this a GC
bug ?