# Finalizer and atexit for C library wrappers

**URL:** https://discourse.julialang.org/t/finalizer-and-atexit-for-c-library-wrappers/772
**Category:** General Usage
**Tags:** question, atexit
**Created:** [December 7, 2016, 1:09am UTC](https://discourse.julialang.org/t/finalizer-and-atexit-for-c-library-wrappers/772 "2016-12-07T01:09:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![friedmud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/friedmud/32/277_2.png) [@friedmud](https://discourse.julialang.org/u/friedmud)
#### Post date: [December 7, 2016, 1:09am UTC](https://discourse.julialang.org/t/finalizer-and-atexit-for-c-library-wrappers/772/1 "2016-12-07T01:09:24Z")

</div>

I have created a module that wraps a large C library. I need to use object finalizers to run teardown functions from the C library for each Julia object I create that’s holding onto structs from the C library.

I addition: the C library needs a final teardown function called before the program exits. It seems natural to do this in an `atexit()` that’s registered in my module’s `__init()__`.

Unfortunately: `atexit()` methods are called _before_ finalizers! This leads to a segfault in my module due to the finalizers calling into the torn-down C library.

What’s the right way to handle this in Julia? Am I going to need to have my own “object registry” that I manually go through and `finalize()` in my `atexit()` routine?

Thanks for any insights!

---

<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: [December 7, 2016, 1:50am UTC](https://discourse.julialang.org/t/finalizer-and-atexit-for-c-library-wrappers/772/2 "2016-12-07T01:50:01Z")

</div>

It’s possible to add post julia finalizer C atexit functions but the thing you can do in it will be very limited (after all, a lot of julia runtime are finalized at that point).

The simplest solution I can think of is to keep a reference count of the library and keep a global/module level pinning object. The refcount should be increased when you construct any object and decreased when the object is finalized (in the finalizer). With this, when the last object is finalized (note that this may not be the module level pinning object since the finalizing order is undefined) you can be sure that julia is done with the library and the library can be finalized.

---

<div class="post-metadata">

### Author: ![friedmud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/friedmud/32/277_2.png) [@friedmud](https://discourse.julialang.org/u/friedmud)
#### Post date: [December 7, 2016, 2:10am UTC](https://discourse.julialang.org/t/finalizer-and-atexit-for-c-library-wrappers/772/3 "2016-12-07T02:10:51Z")

</div>

Hmmm - I can see it… but that’s quite a bit of pain.

What about manually invoking GC during my `atexit()` callback?

At the point where `atexit()` is being called… all of these objects should be unreferenced and GCable. A manual GC call should run all of the finalizers I would think…

Let me try it…

---

<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: [December 7, 2016, 3:37am UTC](https://discourse.julialang.org/t/finalizer-and-atexit-for-c-library-wrappers/772/4 "2016-12-07T03:37:18Z")

</div>

> [@friedmud](#):
>
> all of these objects should be unreferenced and GCable

Not necessarily. There can still be global references.

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [January 21, 2017, 1:17am UTC](https://discourse.julialang.org/t/finalizer-and-atexit-for-c-library-wrappers/772/5 "2017-01-21T01:17:19Z")

</div>

We’ve had to do something like this for the LibGit2 module in Base. See [#20124](https://github.com/JuliaLang/julia/pull/20124) for the changes that were made.
