malloc/segmentation error with ccall

I have interchangeably two errors (either malloc error or segmentation fault) when using ccall to call a C function.

The C function is from the CPLEX C callable library:

CPXLIBAPI
int CPXPUBLIC
   CPXcallbackpostheursoln (CPXCALLBACKCONTEXTptr context, int cnt,
                            int const *ind, double const *val,
                            double obj,
                            CPXCALLBACKSOLUTIONSTRATEGY strat);

My ccall function:

function cbpostheursoln(env::Env,context_::Ptr{Void},cnt::Cint,ind::Vector{Cint},val::Vector{Cdouble},obj::Cdouble,strat::CbSolStrat)
    stat=@cpx_ccall(callbackpostheursoln,Cint,(
    Ptr{Void},
    Cint,
    Ptr{Cint},
    Ptr{Cdouble},
    Cdouble,
    Cint
    ),
    context_,cnt,ind,val,obj,strat)

    println("cbpostheursoln status $stat")

    if stat!=0
        throw(CplexError(env,stat))
    end
    return stat
end

For the errors, I get either of the following two errors:

julia(28377,0x7fffb95f83c0) malloc: *** error for object 0x7fe66b6cfb10: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

OR

signal (11): Segmentation fault: 11

I think candidate bugs are (1) Julia does not convert variable val::Vector{Cdouble} to double const * val correctly; (2) some variables are gabage collected unexpectedly.

For the full code, please check this PR: https://github.com/JuliaOpt/CPLEX.jl/pull/198. To run it, one needs IBM CPLEX v12.8.0 or above.

I have been working on this bug for several hours since last week. I will try to debug with gdb next and will post here if there is new development.

Version information:

Julia Version 0.6.0
Commit 903644385b (2017-06-19 13:05 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, broadwell)

Please don’t judge the Julia version:)

I found the bug in my own code. The malloc error was caused by indexing. I did not convert the Julia 1-index to the C 0-index while using ccall.

Another behavior that could cause a malloc error while using the callback functions in CPLEX C library is that one may forget to restrict the CPLEX solver to use one thread. This will cause problem in the solver side since CPLEX callbacks are not thread safe. This finding should be credited to @odow.

3 Likes