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
More ideas are always welcome!
And thank you for the help so far.
Rémi
Wrap the returned pointer into another type and register a finalizer for it (and you must return it, otherwise it will be garbage collected immediately).
Kirill, I am sure what you are asking. Remi's initial question was resolved to be due to calling print in a finalizer, and unrelated to the ccall usage (which works fine).
Just task switches are disallowed (and thus any IO). Perhaps after the threads branch merged, it'll be worth reconsidering that restriction too. The reason to ban task switch is that gc could happen at any time in user's code due to an allocation. It's not generally advisable therefore to allow task switches at that time, since that it necessary for the user to be much more concerned with syncronization.