MethodError: no method matching unsafe_convert(::Type{Ptr{Int32}}, ::Base.RefValue{Int64})

Hello,

I have an error occur while running an ODE. It didn’t error 3-4 days ago, and I think DifferentialEquations.jl and Sundails.jl had an update in the meantime, so I will need to adjust my code. I think the error is coming from Sundials based on

MethodError: no method matching unsafe_convert(::Type{Ptr{Int32}}, ::Base.RefValue{Int64})
Closest candidates are:
  unsafe_convert(!Matched::Type{Ptr{Nothing}}, ::Base.RefValue{T}) where T at refvalue.jl:30
  unsafe_convert(::Type{T}, !Matched::T) where T<:Ptr at essentials.jl:393
  unsafe_convert(::Type{P}, !Matched::Ptr) where P<:Ptr at essentials.jl:394
  ...
in top-level scope at Creating clearance graphs.jl:58
in collect at base\array.jl:665
in iterate at base\generator.jl:47 
in #112 at base\none 
in run_one_foodweb_no_pristine at test for clearance comparison error.jl:113
in collect at base\array.jl:665
in iterate at base\generator.jl:47 
in  at base\none
in apply_clearance at test for clearance comparison error.jl:79
in  at DiffEqBase\gzrxg\src\solve.jl:104
in #solve#453 at DiffEqBase\gzrxg\src\solve.jl:106 
in solve_up##kw at DiffEqBase\gzrxg\src\solve.jl:110 
in #solve_up#454 at DiffEqBase\gzrxg\src\solve.jl:116
in solve_call##kw at DiffEqBase\gzrxg\src\solve.jl:69 
in #solve_call#450 at DiffEqBase\gzrxg\src\solve.jl:96
in  at Sundials\mWUX7\src\common_interface\solve.jl:10
in __solve##kw at Sundials\mWUX7\src\common_interface\solve.jl:10 
in __solve##kw at Sundials\mWUX7\src\common_interface\solve.jl:10 
in __solve##kw at Sundials\mWUX7\src\common_interface\solve.jl:10 
in __solve##kw at Sundials\mWUX7\src\common_interface\solve.jl:10 
in #__solve#22 at Sundials\mWUX7\src\common_interface\solve.jl:12
in solve! at Sundials\mWUX7\src\common_interface\solve.jl:1190
in get_iters! at Sundials\mWUX7\src\common_interface\solve.jl:1159
in CVodeGetNumSteps at Sundials\mWUX7\src\API\cvodes.jl:188

I don’t know what is being converted that is unsafe. I also don’t know how to make a MWE for this as I use a few files to define what goes into the differential equation. Is there anything obvious? Alternatively, is there a way to revert to a previous version of Sundials or DifferentialEquations?

function apply_clearance(bioS, clearance_V_type, target_species)

    p = (D, S, K, v, r, X, num_nutrient, num_plant, num_animal, num_vessel_types, bm, foodWeb, b, q, ω, c, h, e, target_species, catch_max, b0V, μ, elasticity, clearance_V_type)

    function condition(out, u, t, integrator)
        out .= bioS[1:(end-1)] .- 1e-6
    end

    function affect!(integrator, event_index)
        integrator.bioS[event_index] = 0.0
    end

   extinction_cb = VectorContinuousCallback(condition,affect!,length(bioS[1:(end-1)]))

    fishing_prob = ODEProblem(dBdT,bioS,tspan,p)
    fishing_sol = solve(fishing_prob, save_everystep=false, alg = CVODE_BDF(), abstol=1e-6,reltol=1e-3, callback=extinction_cb)

end

You can install certain versions of packages by entering the PKG mode (pressing ])
and entering e.g.,

add Sundials@4.2.3

To see which versions are available it is helpful to look at the corresponding github page (googling the package name and adding .jl ).
On a more personal note: I would strongly advice organizing your work into environments, projects, and packages. Doing so would alleviates such versioning problems.
For a start you can have a look here:
https://julialang.github.io/Pkg.jl/v1/getting-started/
or as it seems that you are doing science maybe have a look here:

It’s a bug in Sundails.jl on windows. https://github.com/SciML/Sundials.jl/blob/66a733a3d2064f43177337986afb07df5d72fea5/src/common_interface/solve.jl#L1170 introduced in https://github.com/SciML/Sundials.jl/pull/270/commits/b0578dc2da3399633c4e4cfb5ec437d66979815d assumes that Clong is Int, which is not true on windows 64bit.

@Rudi79 Thank you for your reply.

Reverting to the previous version of Sundials.jl or DiffEqBase.jl does not prevent the error from happening, which means something else is causing the issue.

I will look into DrWatson.jl as you are the second person to recommend it to me.

@yuyichao Thank you for your reply. Sometimes it’s nice to learn it is a code issue, and not something one has done :slight_smile: Is there a way around this bug?

Submit a private and fix it. It’s a trivial fix and you can find other correct way to do it by just searching Ref in that file.

I don’t understand what is meant by submit private.

I found Ref in the link you provided. I assume you mean this part of the code?

function DiffEqBase.solve!(integrator::AbstractSundialsIntegrator)
    uType = eltype(integrator.sol.u)
    iters = Ref(-1)
    while !isempty(integrator.opts.tstops)

but I don’t know what to do or how I would access that line of code within my computer. I am a really basic programmer.

I mean a pr
And I meant changing it to match other ref in the same file…

I’m sorry, I still don’t understand what is meant by pr.

So I should change Ref(Clong(-1)) to Ref(Int(-1)) ?

I made a new release with the fix. Could you update?

It works! Thanks!