I have a couple data types which hold a reference to a GPU pointer, where some
data is allocated, as well as a non managed pointer which points somewhere
inside this data array. The definition looks like
type CudaVector*[A] = object
data*: ref[ptr A]
fp*: ptr A
len, step*: int32
and there is [a similar one for
matrices](https://github.com/unicredit/neo/blob/master/neo/cudadense.nim#L18-L27).
The idea is that many vectors and matrices can share the same data but point
into different places inside this data. (Here `fp` is the actual inner pointer,
while `data` is a reference to a pointer to the beginning of the data.) When
the last reference to the data goes away, I want to clean it up. Hence there is
[this
finalizer](https://github.com/unicredit/neo/blob/master/neo/cudadense.nim#L33-L35)
proc freeDeviceMemory[A](p: ref[ptr A]) =
if not p[].isNil:
check cudaFree(p[])
Each time I create a new vector or matrix, either:
* `data` is initialized [using the
finalizer](https://github.com/unicredit/neo/blob/master/neo/cudadense.nim#L46),
or
* `data` is [copied from a pre-existing vector or
matrix](https://github.com/unicredit/neo/blob/master/neo/cudadense.nim#L134)
When launching the tests, though, I get an error while locating the finalizer:
Traceback (most recent call last)
unittest.nim(405) scopy
gc.nim(550) newSeqRC1
gc.nim(525) newObjRC1
gc.nim(245) prepareDealloc
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
The [relevant
line](https://github.com/nim-lang/Nim/blob/devel/lib/system/gc.nim#L245) is
if t.finalizer != nil:
Similarly with gc v2:
Traceback (most recent call last)
unittest.nim(404) scopy
unittest.nim(307) testEnded
unittest.nim(126) testEnded
unittest.nim(193) testEnded
repr.nim(81) reprEnum
gc2.nim(479) newObjNoInit
gc2.nim(454) rawNewObj
gc2.nim(961) collectCT
gc2.nim(927) collectCTBody
gc2.nim(897) collectZCT
gc2.nim(224) prepareDealloc
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
The [relevant
line](https://github.com/nim-lang/Nim/blob/devel/lib/system/gc2.nim#L224) is
if cell.typ.finalizer != nil:
Any ideas what I am doing wrong?