# Running something atexit after all finalizer

**URL:** <https://discourse.julialang.org/t/running-something-atexit-after-all-finalizer/66778>\
**Category:** General Usage\
**Tags:** atexit\
**Created:** [August 21, 2021, 1:46pm UTC](https://discourse.julialang.org/t/running-something-atexit-after-all-finalizer/66778 "2021-08-21T13:46:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 21, 2021, 1:46pm UTC](https://discourse.julialang.org/t/running-something-atexit-after-all-finalizer/66778/1 "2021-08-21T13:46:10Z")

</div>

I have some finalizers  
they need to `ccall` in order to free some external resources.  
I also have a function I need to call right at the end which completes the tear-down – it undoes something I do in ` __init__ `.  
That at-exit function removes parts that are need to run the finalizers.

Right now I am getting errors because the `atexit` is running before the finalizers.  
Which means the finalizers error

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [August 21, 2021, 2:18pm UTC](https://discourse.julialang.org/t/running-something-atexit-after-all-finalizer/66778/2 "2021-08-21T14:18:51Z")

</div>

Is it feasible to check whether the resource is released in the finalizers?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 21, 2021, 3:24pm UTC](https://discourse.julialang.org/t/running-something-atexit-after-all-finalizer/66778/3 "2021-08-21T15:24:37Z")

</div>

That is is what I am doing right now. Works for me.  
But it seems like a nonideal solution – what if I really did need those finalizers to run?  
Like if they were controlling some physical device.  
(One probably shouldn’t do that with finalizers, since a hard crash would prevent anything running, but it might make sense sometimes as a extra check)

Perhaps less weirdly, if they were going to free some element in a collection.  
And the `atexit` was going to free the collection.  
So freeing the collection without freeing the elements would leak memory.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [August 21, 2021, 3:36pm UTC](https://discourse.julialang.org/t/running-something-atexit-after-all-finalizer/66778/4 "2021-08-21T15:36:27Z")

</div>

What’s the way to do resource management in the C/C++ library? It’s better to follow the same style, sometimes we may need to implement (intrusive) reference-counting manually though.
