I'm using chipmunk7 for a project, and have ran into an issue using repr on an object with a PolyShape.
This doesn't seem to be particular to this one type.
I'm unsure if this is a problem with my code, the wrapper, or a Nim bug.
Example:
import chipmunk7
type Foo* = ref object
poly: PolyShape
proc newVect(x, y: float): Vect =
Vect(x: x, y: y)
when isMainModule:
var verts = @[newVect(4, 4), newVect(4, 0), newVect(0, 0), newVect(0, 4)]
let myShape = newPolyShape(
newBody(1.0, 1.0),
cint verts.len,
addr verts[0],
cfloat 0
)
let foo = Foo(poly: myShape)
# Cannot repr an object with a property that comes from a C lib.
echo repr(foo)
Output:
:!nim r /home/avahe/programming/nim/bug/src/bug.nim
Hint: used config file '/home/avahe/.choosenim/toolchains/nim-1.4.8/config/nim.cfg' [Conf]
Hint: used config file '/home/avahe/.choosenim/toolchains/nim-1.4.8/config/config.nims' [Conf]
........CC: bug.nim
Hint: [Link]
Hint: 28174 lines; 0.220s; 31.805MiB peakmem; Debug build; proj: /home/avahe/programming/nim/bug/src/bug.nim; out: /home/avahe/.cac
he/nim/bug_d/bug [SuccessX]
Hint: /home/avahe/.cache/nim/bug_d/bug [Exec]
Traceback (most recent call last)
/home/avahe/programming/nim/bug/src/bug.nim(19) bug
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(327) reprAny
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(266) reprAux
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(240) reprRef
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(259) reprAux
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(217) reprRecord
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(200) reprRecordAux
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(266) reprAux
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(240) reprRef
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(262) reprAux
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(217) reprRecord
/home/avahe/.choosenim/toolchains/nim-1.4.8/lib/system/repr.nim(195) reprRecordAux
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
If anyone could point me in the right direction I'd appreciate it. TIA
I'm actually needing to use the addr of the object, but it breaks in a similar fashion. I should have mentioned that.
Illegal storage access when doing foo[].addr
you can:
echo cast[pointer](foo).repr # address on the heap
echo cast[pointer](foo.poly).repr #doesn't look like a heap address, more like an address into the binary or the library or something...
if the wrapper exposed the types a bit more:
import chipmunk7
proc newVect(x, y: float): Vect =
Vect(x: x, y: y)
type
CPBody = ptr object
velfunc,posfunc: pointer
m,minv: float
i,iinv: float
CPShapeMassInfo = object
m,i:float
cog:Vect
area:float
CPShapeFilter = object
group:pointer
categories,bitmask:uint32
CPShapeType = enum
cpCIRCLE,cpSEGMENT,cpPOLY,cpNUM
CPShapeClass = object
`type`:CPShapeType
cacheData,destroy,pointQuery,segmentQuery: pointer
CPBB = object
l,b,r,t:float
CPShape{.inheritable,pure.} = object
klass:ptr CPShapeClass
space:pointer
body: CPBody
massInfo: CPShapeMassInfo
bb: CPBB
sensor: bool
e,u:float
surfaceV:Vect
userData:pointer
`type`:uint
filter:CPShapeFilter
next,prev: ptr CPShape
hashid:uint
CPSplittingPlane = object
v0,n:Vect
CPPolyShape = ptr object of CPShape
r:float
count:int
planes:pointer
underscore_planes:UncheckedArray[CPSplittingPlane]
Foo* = ref object
poly: CPPolyShape
when isMainModule:
var verts = @[newVect(4, 4), newVect(4, 0), newVect(0, 0), newVect(0, 4)]
let myShape = newPolyShape(
newBody(1.0, 1.0),
cint verts.len,
addr verts[0],
cfloat 0
)
let foo = Foo(poly:cast[CPPolyShape](myShape))
echo foo.repr
the problem is in the wrapper. it declares
type
ShapeObj{.inheritable.} = object
Shape = ptr ShapeObj
PolyShape = object of Shape
ShapeObj needs to be declared {.pure.} (as i've done above) if this fake inheritance is to be used.
The underlying objects are plain c, and use encapsulation, not inheritance, i.e.
typedef struct cpShape {
//some stuff
}cpShape
typedef struct cpPolyShape {
cpShape shape;
//more stuff
}cpPolyShape;