Preventing double-free error with `stablePtr`

Simon Peyton Jones simonpj at
Tue Feb 6 09:11:37 UTC 2018

Catching double-frees does sound like a good idea to me.

Also, is the wisdom in Roman’s post captured in the StablePtr docs?  If not, can someone do that?


From: Libraries [mailto:libraries-bounces at] On Behalf Of Gershom B
Sent: 05 February 2018 01:38
To: Haskell Libraries <libraries at>
Subject: Preventing double-free error with `stablePtr`

I was just reading Roman Cheplyaka’s very interesting blog-post here:<>.

As he points out, the docs for `freeStablePtr` say

"if the stable pointer is passed to deRefStablePtr or freeStablePtr, the behaviour is undefined.”

And indeed we can observe weird behavior as a result of sucn an error.

A deRef of a stable pointer is arguably the sort of sharp-edge we know how to code to avoid. But a double free is a bit trickier. Would it be worth adding a bit more overhead to make such an operation idempotent?

Additionally, would it be worthwhile to add `withStablePtr` to the `Foreign.StablePtr` module? I imagine there are cases that it won’t cover, but it would at least encourage good discipline in the cases that it does handle. The evident utility of such a function is witnessed by its existence in a few different codebases, not least the Win32 library (<>)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Libraries mailing list