Skip to content

Drop ManuallyDrop#2361

Merged
kennykerr merged 2 commits intomasterfrom
ManuallyDrop
Mar 2, 2023
Merged

Drop ManuallyDrop#2361
kennykerr merged 2 commits intomasterfrom
ManuallyDrop

Conversation

@kennykerr
Copy link
Copy Markdown
Collaborator

Thanks to #2360, I can now get rid of the weird custom ManuallyDrop in favor of the standard type. Previously, the parameter binding support depended on a custom ManuallyDrop implementation.

Fixes: #2294

@kennykerr kennykerr merged commit 23e0800 into master Mar 2, 2023
@kennykerr kennykerr deleted the ManuallyDrop branch March 2, 2023 17:51
Anonymous: D3D12_RESOURCE_BARRIER_0 {
Transition: std::mem::ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER {
pResource: ManuallyDrop::new(resource),
pResource: unsafe { std::mem::transmute_copy(resource) },
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennykerr what's the guidance for this? Seems the typical user might go for a safe ManuallyDrop::new(Some(resource.clone())), not immediately realizing that this keeps the refcount on resource up, leaking it.

IMO the previous ManuallyDrop implementation was clearer in terms of sneakily holding a borrow for which the refcount wasn't increased.

Perhaps a much more invasive change would be having lifetimes on structs: &'a X has the same layout as *const X/*mut X.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I addressed that briefly here: #2386 - while the custom ManuallyDrop was more concise, it did hide the fact that you were doing something very dangerous by sharing the reference. I'd love to use lifetimes but yes that's way more invasive and in many cases the lifetime isn't easy to reason about. So here at least we're acknowledging you need to pay close attention. 🤔

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just saw your example was cloning - yes that would be bad. 😅

I don't know what to say - hopefully developers using Direct3D know enough about COM to realize that would be wrong. Direct3D also has leak detection support so that should help catch bugs like that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here at least we're acknowledging you need to pay close attention.

You already seem to have figured it out, and I hope that most Rust programmers' alarm bells ring upon seeing ManuallyDrop::new(x.clone()) (in whatever context), but it is still rather easy to make this mistake as it's the only safe way to write this. Even worse when already having ownership of x and not needing to clone at all making it rather invisible (apart needing to call ManuallyDrop::new()) 😬

In my own code I didn't see the leak detector triggering (on resources), only resource overlapping further down the line - might have to dig a bit further.

I addressed that briefly here: #2386

Great to have that, even better if it could be part of some obvious/up-front documentation? Or an API that nudges towards this altogether? Not sure how it would look like, or if borrows would be more beneficial in the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to use core::ManuallyDrop

2 participants