[Haskell-cafe] Allocating a new ByteString
Yuras Shumovich
shumovichy at gmail.com
Tue Dec 23 06:47:07 UTC 2014
On Tue, 2014-12-23 at 08:19 +0200, Tristan Seligmann wrote:
> On 23 December 2014 at 08:10, Yuras Shumovich <shumovichy at gmail.com> wrote:
> > Looks like you need 'unsafePackCStringFinalizer' (or one of it's variants)
> > from Data.ByteString.Unsafe module. It lets you reuse the buffer and
> > deallocate it in finalizer. Something similar is used e.g. in 'kyotocabinet'
> > package.
>
> I tried using this, but it seems like the pointer created by
> mallocForeignPtrBytes is freed as soon as it is no longer referenced
> (upon returning from the function). Then, later, when the bytestring
> is finalized, the pointer is freed again causing an abort on
> double-free. How do I "transfer" ownership of the pointer?
'mallocForeignPtrBytes' attaches finalizer to the 'ForeignPtr', but you
need to attach it to 'ByteString'. So you can't use
'mallocForeignPtrBytes', but plain old 'malloc' (from
'Foreign.Marshal.Alloc') should work in combination with
'unsafePackMallocCStringLen'. Unless I'm missing something.
Thanks,
Yuras
More information about the Haskell-Cafe
mailing list