Why is adding/removing a reference to a SafeHandle
"dangerous"?
#101980
-
When using Win32, many P/Invoked functions take a Is there a reason for them being labeled as "dangerous"? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Because once you call add, the handle will be leaked unless a corresponding release call is made, and finalization won't help. And because anyone who has that reference can call release even if they didn't call add, which means code that's using the handle correctly can still experience use-after-free bugs if code elsewhere misuses that instance. The naming is just there to say "be really careful here, there be dragons". |
Beta Was this translation helpful? Give feedback.
Because once you call add, the handle will be leaked unless a corresponding release call is made, and finalization won't help. And because anyone who has that reference can call release even if they didn't call add, which means code that's using the handle correctly can still experience use-after-free bugs if code elsewhere misuses that instance. The naming is just there to say "be really careful here, there be dragons".