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.
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
proc freeDeviceMemory[A](p: ref[ptr A]) =
if not p[].isNil:
check cudaFree(p[])
Each time I create a new vector or matrix, either:
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 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 is
if cell.typ.finalizer != nil:
Any ideas what I am doing wrong?