# malloc/segmentation error with ccall

**URL:** <https://discourse.julialang.org/t/malloc-segmentation-error-with-ccall/17653>\
**Category:** General Usage\
**Created:** [November 17, 2018, 7:01pm UTC](https://discourse.julialang.org/t/malloc-segmentation-error-with-ccall/17653 "2018-11-17T19:01:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![g001234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g001234/32/2956_2.png) [@g001234](https://discourse.julialang.org/u/g001234)\
**Post date:** [November 17, 2018, 7:01pm UTC](https://discourse.julialang.org/t/malloc-segmentation-error-with-ccall/17653/1 "2018-11-17T19:01:55Z")

</div>

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:

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

```

My `ccall` function:

```julia
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
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

```julia
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](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
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:)

---

<div class="post-metadata">

**Author:** ![g001234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g001234/32/2956_2.png) [@g001234](https://discourse.julialang.org/u/g001234)\
**Post date:** [December 23, 2018, 4:08pm UTC](https://discourse.julialang.org/t/malloc-segmentation-error-with-ccall/17653/2 "2018-12-23T16:08:28Z")

</div>

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.
