hyperdelia
hyperdelia

Reputation: 1145

Julia's garbage collection of ccall allocated data

I was hoping someone could clarify one aspect of the behaviour of the Julia garbage collector and how it interacts with memory allocated by a call to a C function using ccall.

For example, I am making the following call:

    setup::Ptr{Void} = ccall(("vDSP_DCT_CreateSetup", libacc), Ptr{Void},
                         (Ptr{Void}, UInt64, UInt64),
                         previous, length, dct_type)

This function allocates memory and initializes memory for a DFT_Setup object (the details of this are irrelevant). The library also provides a destructor to be called on the DFT_Setup to deallocate memory once the object is no longer needed.

Is calling the destructor necessary in Julia? i.e. Does the garbage collector handle freeing DFT_Setup when it is appropriate, or should I make a call to the C deallocator?

Upvotes: 4

Views: 273

Answers (1)

Simon Byrne
Simon Byrne

Reputation: 7874

Yes, the Julia GC can only clean up the memory allocated by Julia itself, it has no knowledge of memory allocated by ccalls.

The usual way to solve this is to call the destructor from the finalizer, defined when in the constructor, e.g. see RCall.jl.

Upvotes: 5

Related Questions