# Is parent object alive for duration of \`withUnsafePointer(to:)\` instance member?

**URL:** <https://forums.swift.org/t/is-parent-object-alive-for-duration-of-withunsafepointer-to-instance-member/47342>\
**Category:** Using Swift\
**Created:** [April 12, 2021, 2:45am UTC](https://forums.swift.org/t/is-parent-object-alive-for-duration-of-withunsafepointer-to-instance-member/47342 "2021-04-12T02:45:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [April 12, 2021, 2:45am UTC](https://forums.swift.org/t/is-parent-object-alive-for-duration-of-withunsafepointer-to-instance-member/47342/1 "2021-04-12T02:45:59Z")

</div>

if i have a class

```swift
class Foo 
{
    var value:Bar

    ...
    deinit 
    {
        // deinitialize `self.value`
    }
}

```

and i take an unsafe pointer to `value`

```swift
withUnsafePointer(to: self.value) 
{
    ...
}

```

is the instance of `Foo` guaranteed to be alive for the duration of the `withUnsafePointer` call? or must it be wrapped in a `withExtendedLifetime(_:)` fence?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [April 12, 2021, 6:04pm UTC](https://forums.swift.org/t/is-parent-object-alive-for-duration-of-withunsafepointer-to-instance-member/47342/2 "2021-04-12T18:04:40Z")

</div>

Today, `withUnsafePointer(to:)` does no lifetime extension, and for that matter neither does `withUnsafeMutablePointer(to:)`. The lifetime behavior comes entirely from the `&`, i.e. `inout` semantics, which applies whenever you use `&` to pass something as a pointer. So in your example, there’s no `inout`, and thus the formal behavior here is:

1. Read the property
2. Pass that _value_ to `withUnsafePointer(to:)`
3. `withUnsafePointer(to:)` gets the address of its parameter (which may mean storing it into a temporary)
4. That address gets passed to the callback as a pointer

Optimization will usually collapse steps 1-3 so that you end up with the address of the stored property instead of some temporary, but it’s not part of the formal semantics. This isn’t going to be a dangling pointer, but neither is the base object guaranteed to stay alive.

Now, these functions might be special enough that they should be blessed with additional behavior, but today they are not.

---

<div class="post-metadata">

**Author:** ![Andrew\_Trick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew_trick/32/323_2.png) [@Andrew\_Trick](https://forums.swift.org/u/Andrew_Trick)\
**Post date:** [April 27, 2021, 6:27pm UTC](https://forums.swift.org/t/is-parent-object-alive-for-duration-of-withunsafepointer-to-instance-member/47342/3 "2021-04-27T18:27:51Z")

</div>

@lorentey has proposed stable storage attribute for stored properties. If Swift had such a thing, then you could guarantee that an `inout` gets the stored property address, not a temporary address.
