<br><br><div class="gmail_quote">On Mon, Jun 20, 2011 at 1:46 PM, Stephen Blackheath [to GHC-iPhone] <span dir="ltr">&lt;<a href="mailto:likeliest.complexions.stephen@blacksapphire.com">likeliest.complexions.stephen@blacksapphire.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">David,<br>
<br>
I hadn&#39;t quite grasped your code. The problem with your code is that these two lines...<br>
<br>
&gt;                                     func &lt;- funky<br>
&gt;                                     freeHaskellFunPtr func<br>
<br>
...allocate then deallocate a function pointer, with a net result of nothing useful, and no effect on the function pointer you allocated on the third line.<br>
<br>
If you use this trick, however...<br>
<br>
{-# LANGUAGE DoRec #-}<br>
<br>
foreign import ccall safe &quot;wrapper&quot; mkInit<br>
    :: (Ptr WorkerThread -&gt; CInt -&gt; IO ()) -&gt; IO (FunPtr (Ptr WorkerThread -&gt; CInt -&gt; IO ()))<br>
<br>
applicationMain init dealloc = mdo<br>
    fInit &lt;- mkInit $ \callbackWorker fd -&gt; do<br>
        freeHaskellFunPtr fInit<br>
        ...<br>
    ...<br>
<br>
...then you can do it quite painlessly.  Note that DoRec is correct for ghc 6.10, but its name has changed in later ghc versions.  The compiler will tell you.<br></blockquote><div><br><br>Excellent!  Thanks for the &quot;pointer&quot; oh wait... &quot;thunk&quot; oh wait... ;-)<br>
 </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
<br>
Steve<div class="im"><br>
<br>
On 21/06/11 05:00, David Pollak wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
<br>
<br>
On Sun, Jun 19, 2011 at 1:32 PM, Stephen Blackheath [to GHC-iPhone]<br>
&lt;<a href="mailto:likeliest.complexions.stephen@blacksapphire.com" target="_blank">likeliest.complexions.<u></u>stephen@blacksapphire.com</a><br></div><div><div></div><div class="h5">
&lt;mailto:<a href="mailto:likeliest.complexions.stephen@blacksapphire.com" target="_blank">likeliest.complexions.<u></u>stephen@blacksapphire.com</a>&gt;&gt; wrote:<br>
<br>
    David,<br>
<br>
    In GHC-iPhone the number of function pointers that are allowed to be<br>
    active for a given function is limited, but you can change the size<br>
    of the limit.  This is documented in<br>
<br>
<br>
Steve,<br>
<br>
Yep... I understand that there are not an unlimited number of function<br>
pointers.  However, the function that I wrote was &quot;supposed&quot; to<br>
automatically release the function:<br>
<br>
runOnMain :: IO () -&gt; IO ()<br>
runOnMain todo = do<br>
                   func &lt;- funky<br>
                   dispatchFunc func<br>
   where funky =  mkStrCB $ \v -&gt; do<br>
                                    todo<br>
                                    func &lt;- funky<br>
                                    freeHaskellFunPtr func<br>
<br>
Note the freeHaskellFunPtr function call at the end of the lambda.<br>
<br>
I&#39;m thinking that the &quot;func &lt;- funky&quot; line is being treated as a<br>
recursive call and I&#39;m also thinking that this is a limitation of my<br>
understanding the magic of lazy evaluation.<br>
<br>
So, please let me rephrase my question:<br>
<br>
How do I get the pointer to a wrapper function so that the pointer can<br>
be used within the wrapped function itself?<br>
<br>
I have worked around the particular issue by making a call to<br>
freeHaskellFunPtr in the Objective-C code that invokes the function.<br>
It&#39;s less optimal that being able to have the Haskell code be able to<br>
auto-free the function pointer after the function is invoked.<br>
<br>
Thanks,<br>
<br>
David<br>
<br>
    <a href="http://projects.haskell.org/__ghc-iphone/downloads/GHC-__iPhone.pdf" target="_blank">http://projects.haskell.org/__<u></u>ghc-iphone/downloads/GHC-__<u></u>iPhone.pdf</a><br>
    &lt;<a href="http://projects.haskell.org/ghc-iphone/downloads/GHC-iPhone.pdf" target="_blank">http://projects.haskell.org/<u></u>ghc-iphone/downloads/GHC-<u></u>iPhone.pdf</a>&gt;<br>
<br>
    on p5 under &#39;Pool sizes for foreign function &quot;wrapper&quot; callbacks&#39;.<br>
<br>
<br>
    Steve<br>
<br>
<br>
    On 18/06/11 11:29, David Pollak wrote:<br>
<br>
        Howdy,<br>
<br>
        I tried to build a function that would create an auto-freeing<br>
        function<br>
        pointer:<br>
<br>
        runOnMain :: IO () -&gt; IO ()<br>
        runOnMain todo = do<br>
                           func &lt;- funky<br>
                           dispatchFunc func<br>
           where funky =  mkStrCB $ \v -&gt; do<br>
                                            todo<br>
                                            func &lt;- funky<br>
                                            freeHaskellFunPtr func<br>
<br>
        Unfortunately, I wind up getting:<br>
          internal error: iPhoneCreateAdjustor - adjustor pool<br>
        &#39;Main_d2K2&#39; is<br>
        empty (capacity 32)<br>
             (GHC version 6.10.4-iphone-simulator for i386_apple_darwin)<br>
<br>
        I&#39;m not sure why, but I suspect that funky is being called<br>
        recursively<br>
        and thus stuff is never being freed.<br>
<br>
        Any hints as to the issue and solutions?<br>
<br>
        Thanks,<br>
<br>
        David<br>
<br>
<br>
        --<br>
        Lift, the simply functional web framework <a href="http://liftweb.net" target="_blank">http://liftweb.net</a><br>
        Simply Lift <a href="http://simply.liftweb.net" target="_blank">http://simply.liftweb.net</a><br>
        Follow me: <a href="http://twitter.com/dpp" target="_blank">http://twitter.com/dpp</a><br>
        Blog: <a href="http://goodstuff.im" target="_blank">http://goodstuff.im</a><br>
<br>
<br>
<br>
        ______________________________<u></u>___________________<br>
        iPhone mailing list<br></div></div>
        <a href="mailto:iPhone@haskell.org" target="_blank">iPhone@haskell.org</a> &lt;mailto:<a href="mailto:iPhone@haskell.org" target="_blank">iPhone@haskell.org</a>&gt;<div class="im"><br>
        <a href="http://www.haskell.org/__mailman/listinfo/iphone" target="_blank">http://www.haskell.org/__<u></u>mailman/listinfo/iphone</a><br>
        &lt;<a href="http://www.haskell.org/mailman/listinfo/iphone" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/iphone</a>&gt;<br>
<br>
<br>
    ______________________________<u></u>___________________<br>
    iPhone mailing list<br></div>
    <a href="mailto:iPhone@haskell.org" target="_blank">iPhone@haskell.org</a> &lt;mailto:<a href="mailto:iPhone@haskell.org" target="_blank">iPhone@haskell.org</a>&gt;<div class="im"><br>
    <a href="http://www.haskell.org/__mailman/listinfo/iphone" target="_blank">http://www.haskell.org/__<u></u>mailman/listinfo/iphone</a><br></div><div class="im">
    &lt;<a href="http://www.haskell.org/mailman/listinfo/iphone" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/iphone</a>&gt;<br>
<br>
<br>
<br>
<br>
--<br>
Lift, the simply functional web framework <a href="http://liftweb.net" target="_blank">http://liftweb.net</a><br>
Simply Lift <a href="http://simply.liftweb.net" target="_blank">http://simply.liftweb.net</a><br>
Follow me: <a href="http://twitter.com/dpp" target="_blank">http://twitter.com/dpp</a><br>
Blog: <a href="http://goodstuff.im" target="_blank">http://goodstuff.im</a><br>
<br>
</div></blockquote>
</blockquote></div><br><br clear="all"><br>-- <br>Lift, the simply functional web framework <a href="http://liftweb.net" target="_blank">http://liftweb.net</a><div>Simply Lift <a href="http://simply.liftweb.net" target="_blank">http://simply.liftweb.net</a><br>
Follow me: <a href="http://twitter.com/dpp" target="_blank">http://twitter.com/dpp</a><br>Blog: <a href="http://goodstuff.im" target="_blank">http://goodstuff.im</a><br></div><br>