# Segfault when calling library function that works correctly standalone

**URL:** <https://discourse.julialang.org/t/segfault-when-calling-library-function-that-works-correctly-standalone/30189>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 22, 2019, 9:07pm UTC](https://discourse.julialang.org/t/segfault-when-calling-library-function-that-works-correctly-standalone/30189 "2019-10-22T21:07:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [October 22, 2019, 9:07pm UTC](https://discourse.julialang.org/t/segfault-when-calling-library-function-that-works-correctly-standalone/30189/1 "2019-10-22T21:07:33Z")

</div>

So there’s this very basic [example](https://pastebin.com/aVwM7j3i) of finding the roots of unity using the [MPSolve](https://github.com/robol/MPSolve) library. Calling from C

```c
void test_mps(void);

int main(void)
{
  test_mps();
  return 0;
}

```

gives the correct results and passes valgrind check. However, when called from Julia

```julia
ccall((:test_mps, "./libmps_test"), Cvoid, ())

```

it prints

> Enter solver

and then fails with “Segmentation error”. What could be causing this behaviour? The library uses libpthread internally, perhaps it conflicts with jula runtime somehow?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 22, 2019, 9:20pm UTC](https://discourse.julialang.org/t/segfault-when-calling-library-function-that-works-correctly-standalone/30189/2 "2019-10-22T21:20:23Z")

</div>

Here are very little chance of conflict caused by pthread.

The more likely reason is for c bug to be triggered when called in a less clean context, or if encode is using gmp/mpfr, the allocator Julia supplies to it can’t run on unmanaged thread (even though it’s thread safe). I think the second issue was fixed recently.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [October 22, 2019, 9:25pm UTC](https://discourse.julialang.org/t/segfault-when-calling-library-function-that-works-correctly-standalone/30189/3 "2019-10-22T21:25:52Z")

</div>

The library in question uses GMP internally. Where the “second issue” has been fixed? The latest GMP release is from 2016, julia version is 1.2

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 22, 2019, 9:33pm UTC](https://discourse.julialang.org/t/segfault-when-calling-library-function-that-works-correctly-standalone/30189/4 "2019-10-22T21:33:27Z")

</div>

The fix is in Julia.im outside and couldn’t easily check…

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [October 22, 2019, 9:34pm UTC](https://discourse.julialang.org/t/segfault-when-calling-library-function-that-works-correctly-standalone/30189/5 "2019-10-22T21:34:47Z")

</div>

Do you mean that it could be in v1.3.0-rc4 ?

EDIT: Yep, works after upgrading to 1.3.0-rc4. Thanks for the tip
