Calling c function from julia - segmentation fault

It contains some preprocessing required by the C function.
Since the function is written in pure Julia I figured it is unlikely to be the source of the problem.

# Setup edgelists for cvxclustr c functions
function compactify_edges(w, n)

    sizes1 = zeros(Int64, n)
    sizes2 = zeros(Int64, n)
    
    # ix is a the list (i j) where w[i, j] != 0
    P = reduce(vcat, [i j] for i ∈ 1:n for j ∈ i+1:n)
    P = P[w .!=0, :] # remove elements where W is zero


    M1 = zeros(Int64, size(w, 1), n)
    M2 = zeros(Int64, size(w, 1), n)

    for i in 1:n
        group1 = findall(P[:,1] .== i)
        sizes1[i] = length(group1)
        if (sizes1[i] > 0) 
          M1[1:sizes1[i], i] = group1
        end

        group2 = findall(P[:,2] .== i)
        sizes2[i] = length(group2)
        if (sizes2[i] > 0) 
          M2[1:sizes2[i], i] = group2
        end
    end

    M1 = M1[1:maximum(sizes1), :]
    M2 = M2[1:maximum(sizes2), :]

    return P, M1, M2, sizes1, sizes2
end