Right now I am trying to rewrite my wrapper for VapourSynth:

  * old: [VapourSynth.nim](https://github.com/mantielero/VapourSynth.nim)
  * new: [vs.nim](https://github.com/mantielero/vs.nim) (futhark based, by the 
way)



The old one was moving pointers. Now I am trying to move objects that contain a 
handler with the pointer.

So in the old API I had:
    
    
    proc len*(vsmap:ptr VSMap, key:cstring):int =
    
    
    Run

while in the new API:
    
    
    type
      VSMapObj* = object
        handle*:ptr VSMap
    
    proc `=destroy`*(self: VSMapObj) =
      if not self.handle.isNil:
        api.handle.freeMap(self.handle)  # this is from the C API
    
    ...
    
    proc len*(vsmap:VSMapObj; key:string):int =
    
    
    
    Run

Now I am facing some memory issues, probably due to my bad understanding of the 
topic. When I compile 
[ex06_adding.nim](https://github.com/mantielero/vs.nim/blob/79955f33f1f928ff84de98acb2354eeefbb1e493/tests/ex06_adding.nim#L7)
 it works fine, but then after the execution it tries to destroy an `VSCoreObj` 
instance which uses the same approach as above. I get the error:
    
    
    false
    200 frames written to 'demo.y4m'
    [INFO] destroying VSCoreObj instance
    Traceback (most recent call last)
    /home/jose/src/nimlang/vs.nim/src/lib/libcore.nim(17) ex06_adding
    /home/jose/src/nimlang/vs.nim/src/lib/libcore.nim(21) =destroy
    SIGSEGV: Illegal storage access. (Attempt to read from nil?)
    Error: execution of an external program failed: 
'/home/jose/src/nimlang/vs.nim/tests/ex06_adding'
    
    
    Run

The issue is in [this 
line](https://github.com/mantielero/vs.nim/blob/79955f33f1f928ff84de98acb2354eeefbb1e493/src/lib/libcore.nim#L21),
 but I don't get it. One `VSCoreObj` instance is needed and I create it 
[here](https://github.com/mantielero/vs.nim/blob/79955f33f1f928ff84de98acb2354eeefbb1e493/src/lib/helper.nim#L5)
 for the whole execution. It looks like `=destroy' is called only once since 
the `[INFO] destroying VSCoreObj instance` line only appears once.

Do you see any obvious error?

Reply via email to