Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • Thanks @cdhowie, very good explanation, unfortunately I can't change the trait as it comes from some Android generated code Commented Jan 3, 2023 at 6:16
  • @Mihai I don't think you can do what you're asking then, without the use of unsafe code that has high probability of introducing undefined behavior. Commented Jan 3, 2023 at 6:18
  • As a last resort I'm ok with unsafe as well because open will be called from only one thread Commented Jan 3, 2023 at 6:22
  • 1
    @Mihai Threads aren't relevant. The problem is that the SomeType value could be dropped before the Foo value, which creates a use-after-free bug, and therefore UB. If this is generated code you may wish to file a bug report against the project responsible for the code generator. Commented Jan 3, 2023 at 6:45
  • It seems the API designer does not want a trait implementation to cache the client. Either it is intended as a use-and-forget scenario, or the trait implementation would clone the client, or save some info extracted from the client. Commented Jan 7, 2023 at 5:15