# Unload a shared library

**URL:** https://discourse.julialang.org/t/unload-a-shared-library/5344
**Category:** New to Julia
**Tags:** ccall
**Created:** [August 12, 2017, 12:05am UTC](https://discourse.julialang.org/t/unload-a-shared-library/5344 "2017-08-12T00:05:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)
#### Post date: [August 12, 2017, 12:05am UTC](https://discourse.julialang.org/t/unload-a-shared-library/5344/1 "2017-08-12T00:05:51Z")

</div>

I used `ccall` to run a function in a shared library. Now it’s loaded, and I’d like to unload it (say, to change something in the source and rebuild). `Libdl.dlcose` looked like it might be what I need, but it takes a handle, and I don’t have one. I tried to get one, but it didn’t help me close the library. Can you unload a library loaded in this way?

```julia
x = Libdl.dlopen("name_of_my_library")
Libdl.dlclose(x) # returns false
filter(x->contains(x, "name_of_my_library"), Libdl.dllist()) # Still shows my library as loaded.

```

---

<div class="post-metadata">

### Author: ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)
#### Post date: [August 12, 2017, 3:30pm UTC](https://discourse.julialang.org/t/unload-a-shared-library/5344/2 "2017-08-12T15:30:23Z")

</div>

On unix-like platforms, `dlopen` is reference-counted and the handle is not deleted until the reference count reaches zero. From a quick look, I think `jl_dlopen` doesn’t check handle existence before calling the system `dlopen` (which will increment and return if the library was already opened in the current process). It’s possible – and likely – that there are multiple calls and a reference count greater than 1 if you are using BinDeps.

For diagnostic purposes, you can also call `dlerror` (`jl_dlerror` exists internally but is not exported). `unsafe_string(ccall(:dlerror, Cstring, ()))` should work.

---

<div class="post-metadata">

### Author: ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)
#### Post date: [August 14, 2017, 4:51pm UTC](https://discourse.julialang.org/t/unload-a-shared-library/5344/3 "2017-08-14T16:51:14Z")

</div>

Thanks @ihnorton. If I understand you correctly, then this is why my method isn’t unloading the library. I think we’re still looking for a way to unload it after a ccall then, yes?

---

<div class="post-metadata">

### Author: ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)
#### Post date: [August 28, 2017, 5:34pm UTC](https://discourse.julialang.org/t/unload-a-shared-library/5344/4 "2017-08-28T17:34:15Z")

</div>

If you were calling

```julia
ccall((:my_fcn, "./my_lib.so"), ...)

```

but needed to close the library when done, then do this instead:

```julia
lib = Libdl.dlopen("./my_lib.so")
sym = Libdl.dlsym(lib, :my_fcn)
ccall(sym, ...) # Remaining arguments are the same.
Libdl.dlclose(lib)

```

More on the GitHub issue: [https://github.com/JuliaLang/julia/issues/23459](https://github.com/JuliaLang/julia/issues/23459)
