First of all, at least on Linux the system doesn't search for the shared 
library in the current directory, so I had to patch the code to test it. Second 
of all, you need `GC_ref` in `test` so that, when destroying from the main app, 
the reference counts becomes 0 and the ref actually gets deallocated. Check 
this code: 
    
    
    # app.nim
    import std/[os, dynlib]
    
    type
      Value* = ref object
      P* = proc(): Value {.nimcall.}
    
    when isMainModule:
      var lib = loadLib(getCurrentDir() / "libext.so")
      if isNil(lib):
        quit("Failed to load libext.dynlib!")
      var test: P = cast[P](lib.symAddr("test"))
      if isNil(test):
        quit("Failed to load test proc")
      
      let res = test()
      echo "Is the result nil - ", isNil(res)
    
    
    Run
    
    
    # ext.nim
    import ./app
    
    proc test*(): Value {.exportc, dynlib.} =
      result = Value()
      GC_ref(result)
    
    
    Run

Reply via email to