# gpui_macos: Retain pasteboard objects \(\#61024\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/914e1c9873b6b85bfeedc5b58e8270885bdd532e)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 914e1c9873b6b85bfeedc5b58e8270885bdd532e

Requested commit: 914e1c9873b6b85bfeedc5b58e8270885bdd532e

Commit: 914e1c9873b6b85bfeedc5b58e8270885bdd532e

Tree: 877e34d642165999454eca87e4e640a576a3105c

Author: Prithvish Baidya

Committer: GitHub

## Message

```
gpui_macos: Retain pasteboard objects (#61024)

## Objective

Prevent a use-after-free when a macOS pasteboard wrapper outlives the
autorelease pool in which it was created.

Pasteboard cached its NSPasteboard and custom zed-text-hash and
zed-metadata NSString objects as raw ids. These constructors return
autoreleased objects. A later clipboard read could therefore pass a
dangling type identifier to dataForType and crash in objc_msgSend.

## Solution

- Retain the pasteboard and custom type objects for the Pasteboard
lifetime with StrongPtr.
- Copy NSData contents into owned Rust byte buffers before the
autoreleased data object can expire.
- Add a regression test that drains the creation autorelease pool and
then accesses the retained custom types.

## Testing

- cargo test -p gpui_macos --features runtime_shaders
test_custom_types_survive_creation_autorelease_pool --lib
- Built an optimized macOS embedder against the patched GPUI checkout.

## Self-Review Checklist:

- [x] I have reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks have justifying comments
- [x] The content adheres to Zed UI and icon standards; this change has
no UI
- [x] Tests cover the changed lifetime behavior
- [x] Performance impact has been considered and is acceptable

---

Release Notes:

- Fixed a macOS crash that could occur when reading from the clipboard.

---
AI Disclosure: I ran into this bug on a project where I'm using GPUI.
For my purposes I patched it on my fork I maintain. I had codex (gpt
5.6-sol) send this PR because I think this fix should be upstreamed.
```

## Parents

- [ba1990441dc71e852216311ec7c4e873de710117](https://git.cafe/gitcafe/zed/commit/ba1990441dc71e852216311ec7c4e873de710117?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/914e1c9873b6b85bfeedc5b58e8270885bdd532e?format=markdown)
