[Haskell-cafe] FFI Ptr Question
Michael Jones
mike at proclivis.com
Mon Apr 6 17:35:38 UTC 2015
Donn,
My intent was to hold the strings in a TVar, with a thread keeping it up to date, in this case, filtering, adding, etc.
I need to look at how TVar works. I assume that a read from a TVar produces a value, and if the read from TVar is done in the callback, the pointer has to be freed when the callback exits, or by the C++ code.
I also recall seeing some code in wxWidgets where after the callback to read is made, it calls another callback to set. It may be intended as a way to free memory. If every read is followed by a set, the set could could free the pointer.
Either way, the design has dynamic data getting updated by a thread at a rate of change much larger than human interaction (scrolling).
Mike
On Apr 6, 2015, at 10:47 AM, Donn Cave <donn at avvanta.com> wrote:
> 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.
>
> Donn
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe
mailing list