Where to find info on shared_ptr support?

According to the web site, shared_ptr isn't yet supported. It only (mis-leadingly, IMO) says that these types can be used as value types (which is not useful for types designed with shared_ptr and particularly weak_ptr in mind, and only useful for legacy types — just like in Swift, types usually aren't used both on stack and on heap).

I can also create a shared_ptr and call methods like reset() on the shared_ptr, but there is no way to actually get at the pointed-to object to call methods on it.

But I can't find any more info on what the road map for shared_ptr is? Can anyone point me to more information about future plans? I can't seem to find the magic combination of words to search for.

You should be able to use mySharedPtr.pointee to retrieve the wrapped object. std::shared_ptr defines an T& operator*() const which is mapped to var pointee: T in Swift.

Thank you, that seems to work for non-mutating methods. Seems .pointee is immutable though, so I can only call const methods. I don't suppose there's a .mutablePointee or so?

I found two things that are broken with shared_ptr in Swift:

  1. .pointee unfortunately copies the instance T instead of borrowing it. Is there some way to avoid this copy?
  2. Passing a shared_ptr to a C-style Swift function (like a "callback") causes a heap corruption. Filed a bug report here: Passing a `std::shared_ptr<...>` from C++ to a C-style Swift function crashes: `malloc: Heap corruption detected ***` · Issue #78292 · swiftlang/swift · GitHub
3 Likes