[Solved] Debugging an apparent memory leak

Updating this thread now that I’ve figured out what the problem was. Hopefully this helps somebody coming from Google (hi!).

The problem is not at all related to the code structure I outlined in the previous post, but rather the details of the long_computation function are at fault.

Essentially I wrote a wrapper around a C++ library that does things like

type MyCxxType
    ptr :: Ptr{Void}
end

function MyCxxType()
    ptr = ccall( ... ) # new CxxType()
    output = MyCxxType(ptr)
    finalizer(output, delete)
    output
end

function delete(input::MyCxxType)
    ccall( ... ) # calls delete
end

The ccalls simply call new and delete for the corresponding type in C++.

I believe the problem here is similar to https://github.com/JuliaLang/julia/issues/11698

Essentially the Julia garbage collector doesn’t know how large MyCxxType really is (it thinks it’s just a pointer). Therefore it never feels any pressure to call the object’s finalizer and so nothing ever gets garbage collected. The solution is therefore to not do this. Write the wrapper in a way that you’re not storing Ptr{Void} as a field. It turns out that that was a mistake…

3 Likes