# Cannot make work finalizer

**URL:** <https://discourse.julialang.org/t/cannot-make-work-finalizer/3563>\
**Category:** New to Julia\
**Created:** [May 6, 2017, 7:21pm UTC](https://discourse.julialang.org/t/cannot-make-work-finalizer/3563 "2017-05-06T19:21:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gyslain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyslain/32/4411_2.png) [@Gyslain](https://discourse.julialang.org/u/Gyslain)\
**Post date:** [May 6, 2017, 7:21pm UTC](https://discourse.julialang.org/t/cannot-make-work-finalizer/3563/1 "2017-05-06T19:21:52Z")

</div>

I can’t make work the finalizer. I have this type A :

```julia
type A
  i::Int64
  function A(i::Int64)
    a = new(i)
    println("typeof(a) = $(typeof(a))")
    println("constructor $(a.i)")
    finalizer(a, destructor)
  end
end
destructor(a::A) = println("destructor $(a.i)") 

```

And when I instanciate `a = A(1)`, I get those errors :

typeof(a) = A  
constructor 1  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF  
jl\_uv\_writecb() ERROR: bad file descriptor EBADF

---

<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:** [May 6, 2017, 7:37pm UTC](https://discourse.julialang.org/t/cannot-make-work-finalizer/3563/2 "2017-05-06T19:37:41Z")

</div>

1. You are not actually returning anything from the constructor so `a` is `nothing`
2. You cannot print (or do any task switch) in finalizer, you should be able to see this warning message if you let the finalizer run before other part of the system shuts down.

---

<div class="post-metadata">

**Author:** ![Gyslain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyslain/32/4411_2.png) [@Gyslain](https://discourse.julialang.org/u/Gyslain)\
**Post date:** [May 7, 2017, 12:47pm UTC](https://discourse.julialang.org/t/cannot-make-work-finalizer/3563/3 "2017-05-07T12:47:29Z")

</div>

Hi yuyichao,  
Sorry, I don’t get it. I would like to have this destructor prints things at the end of a scope. Can’t I do this way? Do I have to explicitly call a function at the end of a scope?

---

<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:** [May 7, 2017, 12:56pm UTC](https://discourse.julialang.org/t/cannot-make-work-finalizer/3563/4 "2017-05-07T12:56:52Z")

</div>

No you cannot use normal print functions in finalizers. On master/0.6, `Core.println` etc should work but it doesn’t use the Base show functions so you can format the string yourself and call `Core.println`.

Also note that the finalizer will not be run at the end of a scope. It can be run before or after that, whenever the runtime determines that the value will not be used again.

---

<div class="post-metadata">

**Author:** ![Gyslain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyslain/32/4411_2.png) [@Gyslain](https://discourse.julialang.org/u/Gyslain)\
**Post date:** [May 7, 2017, 9:31pm UTC](https://discourse.julialang.org/t/cannot-make-work-finalizer/3563/5 "2017-05-07T21:31:10Z")

</div>

Ok, thanks. In Julia, is there any other mechanisms to execute some code automatically at the end of a scope (like a destructor in c++) ?

---

<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:** [May 7, 2017, 9:41pm UTC](https://discourse.julialang.org/t/cannot-make-work-finalizer/3563/6 "2017-05-07T21:41:41Z")

</div>

No, but you can use macros or function wrappers (see the `do` syntax for anonymous function) to enforce that.

---

<div class="post-metadata">

**Author:** ![Gyslain](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyslain/32/4411_2.png) [@Gyslain](https://discourse.julialang.org/u/Gyslain)\
**Post date:** [May 7, 2017, 10:02pm UTC](https://discourse.julialang.org/t/cannot-make-work-finalizer/3563/7 "2017-05-07T22:02:51Z")

</div>

Thanks
