Hi,

I'm creating a Julia type that conatins a Mmapped Vector. The intention is 
that the binary file behind the Mmap vector is created when the type is 
initialised and the file is deleted when the gc knows it's out out of 
scope. I'm using a finalizer function to achieve the deletion.

My code is below. The code works during the Julia session (deleting files 
when they are out of scope) but files are not being deleted when the Julia 
session is closed.

For example,

tmp=MmapVectorInt(50,Int[])
tmp=MmapVectorInt(50,Int[])
tmp1=MmapVectorInt(50,Int[])
tmp2=MmapVectorInt(50,Int[])
tmp2=MmapVectorInt(50,Int[])
gc()

Works fine, deleting the file linked to the first tmp and first tmp2.

However, when I end this Julia session only 2 of the 3 remaining files are 
deleted.

There seems to be an inconsistency with how Julia finalizes objects within 
a Julia session and at the end of a session. Can someone explain this or 
offer an alternative approach?
I'm using the 64 bit Windows version of Julia.

Thanks
Lee


function genbinaryfilename()
    sleep(0.001)
    return split(string(time()),'.')[2]*".bin"
end

type MmapVectorInt
    #vector length
    vlen::Int
    #storage array
    vec::Vector{Int}
    #mmap file
    mmapfile::ASCIIString
    function MmapVectorInt(vlen::Int,vec::Vector{Int})
        # Generate MMap file
        mmapfile=genbinaryfilename()
        s = open(mmapfile, "w+")
        tmp=Array(Int,vlen)
        write(s,tmp)
        close(s)

        # Reopen MMap file
        s = open(mmapfile, "r+")
        vec1 = Mmap.mmap(s, Vector{Int}, (vlen,))
        close(s)

        #copy contents from function to vector
        vec1[1:min(vlen,length(vec))]=deepcopy(vec[1:min(vlen,length(vec))])
        #create the instance
        x = new(vlen,Int[],mmapfile)
        #point the vector to the mmapped vector
        x.vec=vec1
        #add the finalizer function
        finalizer(x,DestroyMmapVectorInt)
        #return x
        x
    end
end

function DestroyMmapVectorInt(x::MmapVectorInt)
    #finalize the mmapped vector. (unlinks the file)
    finalize(x.vec)
    if x.mmapfile!=""
        #delete the file
        rm(x.mmapfile)
    end
end

Reply via email to