[Haskell-cafe] FFI Ptr Question

Donn Cave donn at avvanta.com
Mon Apr 6 16:47:54 UTC 2015

Quoth Michael Jones <mike at proclivis.com>,

> The problem is newCWString creates a string that must be freed, and
> wxWidgets does not free it. This results in a memory leak.
> In a real application, I'll probably store a String in a TVar so that
> some thread can keep it up to date. I could use other TVars to hold
> the CWString and free it each time a new value is created. But, it
> would be better if there was some way to guarantee it is freed, even
> with exceptions, etc. The best would be if when the function returns,
> it automatically freed the string.
> Is there a way express the callback function so that it frees the string
> after the return?

Are you sure that's what you want?  I may be misunderstanding "after
the return" - to me, that sounds like right after your getValue callback.
That would be approximately the same as freeing it within the callback,
right after allocation and initialization.  That's not ideal at all.
You'll need to store the string in your application, it seems to me,
and pass that value via the callback rather than allocating a new copy
each time.


More information about the Haskell-Cafe mailing list