# How long are object references guaranteed to live?

**URL:** <https://discourse.julialang.org/t/how-long-are-object-references-guaranteed-to-live/13916>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 23, 2018, 4:18am UTC](https://discourse.julialang.org/t/how-long-are-object-references-guaranteed-to-live/13916 "2018-08-23T04:18:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcmylin](https://avatars.discourse-cdn.com/v4/letter/l/fbc32d/32.png) [@lcmylin](https://discourse.julialang.org/u/lcmylin)\
**Post date:** [August 23, 2018, 4:18am UTC](https://discourse.julialang.org/t/how-long-are-object-references-guaranteed-to-live/13916/1 "2018-08-23T04:18:54Z")

</div>

Relatively simple question: are object references guaranteed to “live” (i.e. retain the object they point to) for the entirety of the lexical scope in which they reside, or is the compiler free to “forget” them in the middle of a function body, after their last use?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 23, 2018, 5:29am UTC](https://discourse.julialang.org/t/how-long-are-object-references-guaranteed-to-live/13916/2 "2018-08-23T05:29:11Z")

</div>

Are you asking about language internals, or `Ref`? Also, an example to make this concrete would be useful.

---

<div class="post-metadata">

**Author:** ![lcmylin](https://avatars.discourse-cdn.com/v4/letter/l/fbc32d/32.png) [@lcmylin](https://discourse.julialang.org/u/lcmylin)\
**Post date:** [August 23, 2018, 6:01am UTC](https://discourse.julialang.org/t/how-long-are-object-references-guaranteed-to-live/13916/3 "2018-08-23T06:01:14Z")

</div>

More so along the lines of language internals. As an example:

```julia
mutable struct Foo
    ...
end

function baz()
    bar = Foo(...)
    bar_ptr = ptr_from_objref(bar)

    # 'safe' use of bar
    func(bar)

    # activities that may allocate memory
    # but which do not cause bar to escape
    ...

    # 'unsafe' use of bar
    func(unsafe_load(bar_ptr))
end

```

My question is, what are the guarantees (if any) about the aliveness of `bar` (and thus the validity of the memory `bar` points to) at the ‘unsafe’ use site? I could personally imagine two scenarios:

1. The object reference bound to `bar` is held on some GC-reachable stack frame for the duration of any call to `baz`, making it perfectly safe to access that object through a non-retaining pointer within the function’s scope.
2. The object reference bound to `bar` may not exist in any GC-reachable location (e.g. it gets popped off the stack) after the last point in the function where it is used, making it unsafe to access that object through a non-retaining pointer after said last usage point.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 23, 2018, 6:13am UTC](https://discourse.julialang.org/t/how-long-are-object-references-guaranteed-to-live/13916/4 "2018-08-23T06:13:53Z")

</div>

AFAIK the documentation that exists on this is [here](https://docs.julialang.org/en/latest/devdocs/llvm/#GC-root-placement-1).

---

<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:** [August 23, 2018, 6:24am UTC](https://discourse.julialang.org/t/how-long-are-object-references-guaranteed-to-live/13916/5 "2018-08-23T06:24:13Z")

</div>

Short summary is neither. It may or may not be live before or after the last use of the variable. Only very few use will guarantee the liveness of the variable, including `cconvert` return value during a `ccall` and `GC.@preserve`.

---

<div class="post-metadata">

**Author:** ![lcmylin](https://avatars.discourse-cdn.com/v4/letter/l/fbc32d/32.png) [@lcmylin](https://discourse.julialang.org/u/lcmylin)\
**Post date:** [August 24, 2018, 1:39am UTC](https://discourse.julialang.org/t/how-long-are-object-references-guaranteed-to-live/13916/6 "2018-08-24T01:39:44Z")

</div>

Okay, that clarifies things a bit. My best interpretation of the docs lines up with @yuyichao’s answer.
