Hi guys,

While creating a tiny wrapper over the C/C++ primesieve 
<https://github.com/kimwalisch/primesieve> library, I encountered an error 
while trying to finalize the Ptr returned by a function. This pointer must 
be freed by another function from the library, so I tried to wrap it into a 
finalizer function but it doesn't work because of the following error: 
"task switch not allowed from inside gc finalizer". Could someone explain 
how this could be done properly? I would try to avoid the returned array if 
possible. Here is the code so far:


const LIB_PRIMESIEVE = "libprimesieve.so"

type C_primesieve_array
    handle::Ptr{Int32}
end

function primes(n::Int)
    size = Cint[0]
    primes_ptr = ccall(
        (:primesieve_generate_primes, LIB_PRIMESIEVE),
        C_primesieve_array,
        (UInt64, UInt64, Ptr{Cint}, Int),
        1, n, size, 3)

    function primesieve_array_finalizer(primesieve_arr::C_primesieve_array)
        ccall(
            (:primesieve_free, LIB_PRIMESIEVE),
            Void, (Ptr{Int32},), primesieve_arr.handle)
    end

    finalizer(primes_ptr, primesieve_array_finalizer)
    return pointer_to_array(primes_ptr.handle, size[1], false)
end


Thank you very much,
Rémi

Reply via email to