<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt"><div style="display: block;" class="yahoo_quoted">I just understood what you meant by this:<br><br style="" class=""> (defmethod translate-to-foreign ((lisp-value cv-matrix) (c-type cv-mat))<br style="" class="" clear="none"> (values (c-pointer lisp-value) lisp-value)<br><br><br>Ok, so great!...I can now mem-aref without adding the c-pointer..so thank you very much for that, but is there a way I can do the same type of thing to let me foreign-alloc it without adding c-pointer.<br><br style="" class=""> <div class="" style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 10pt"> <div class="" style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 12pt"> <div style="" class=""
dir="ltr"> <font style="" class="" face="Arial" size="2"> On Wednesday, April 30, 2014 9:15 AM, Martin Simmons <martin@lispworks.com> wrote:<br style="" class=""> </font> </div> <blockquote class="" style=""> <div style="" class="">>>>>> On Tue, 29 Apr 2014 07:20:08 +0200, Willem Rein Oudshoorn said:<br style="" class="" clear="none">> Cancel-Lock: sha1:p0/p2S+buMdADzjMLfHl6LN5m6k=<br style="" class="" clear="none">> <br style="" class="" clear="none">> Martin Simmons <<a style="" class="" shape="rect" ymailto="mailto:martin@lispworks.com" href="mailto:martin@lispworks.com">martin@lispworks.com</a>> writes:<br style="" class="" clear="none">> <br style="" class="" clear="none">> >>>>>> On Mon, 28 Apr 2014 07:26:07 +0200, Willem Rein Oudshoorn said:<br style="" class="" clear="none">> >> Cancel-Lock: sha1:zqpk08tO/PaCrz3PLTlI+ocAF9E=<br style="" class="" clear="none">> >> <br
style="" class="" clear="none">> >> Joeish W <<a style="" class="" shape="rect" ymailto="mailto:joeish80829@yahoo.com" href="mailto:joeish80829@yahoo.com">joeish80829@yahoo.com</a>> writes:<br style="" class="" clear="none">> >> <br style="" class="" clear="none">> >> > How do I work with the metaobjects(is that what they are called)<br style="" class="" clear="none">> >> > output by the code you gave me...If you can show me how to mem-aref<br style="" class="" clear="none">> >> > one I would really appreciate it<br style="" class="" clear="none">> >> <br style="" class="" clear="none">> >> Sorry for the short answer, but at the moment I am extremely busy.<br style="" class="" clear="none">> >> If you followed the example<br style="" class="" clear="none">> >> <br style="" class="" clear="none">> >> (mem-aref (c-pointer YOUR-POINT) ...)<br
style="" class="" clear="none">> >> <br style="" class="" clear="none">> >> should work.<br style="" class="" clear="none">> ><br style="" class="" clear="none">> > It might be interesting (or scary depending on your point of view) to consider<br style="" class="" clear="none">> > what happens if the variable YOUR-POINT is the last reference to the CLOS<br style="" class="" clear="none">> > object here...the finalizer might free the foreign object before mem-aref is<br style="" class="" clear="none">> > entered if the compiler no longer keeps a pointer to the variable.<br style="" class="" clear="none">> ><br style="" class="" clear="none">> No this is not safe. In general you should never do <br style="" class="" clear="none">> `(c-pointer ...)` outside the low level parts of the bindings, and use<br style="" class="" clear="none">> it very carefully.<br style="" class=""
clear="none">> <br style="" class="" clear="none">> Personally I would try to never use the `c-pointer` method outside<br style="" class="" clear="none">> the `translate-to-foreign` code. And I naively expected that this<br style="" class="" clear="none">> would be safe. But as you point out:<br style="" class="" clear="none">> <br style="" class="" clear="none">> > Or more specifically, can this ever be safe?<br style="" class="" clear="none">> ><br style="" class="" clear="none">> > (mem-aref (c-pointer (point0)) ...)<br style="" class="" clear="none">> <br style="" class="" clear="none">> This is never safe.<br style="" class="" clear="none">> <br style="" class="" clear="none">> I think the easiest fix is to change the <br style="" class="" clear="none">> <br style="" class="" clear="none">> (defmethod translate-to-foreign ((lisp-value cv-matrix) (c-type cv-mat))<br style=""
class="" clear="none">> (c-pointer lisp-value))<br style="" class="" clear="none">> <br style="" class="" clear="none">> code to [UNTESTED CODE]:<br style="" class="" clear="none">> <br style="" class="" clear="none">> <br style="" class="" clear="none">> (defmethod translate-to-foreign ((lisp-value cv-matrix) (c-type cv-mat))<br style="" class="" clear="none">> (values (c-pointer lisp-value) lisp-value)<br style="" class="" clear="none">> <br style="" class="" clear="none">> <br style="" class="" clear="none">> This should keep the lisp-value around until we are done using the<br style="" class="" clear="none">> `(c-pointer ...)` value. Provided of course you are not mucking around<br style="" class="" clear="none">> with the `c-pointer` method yourself.<br style="" class="" clear="none">> <br style="" class="" clear="none">> <br style="" class=""
clear="none">> In general, I would advocate to hide all the nasty c-pointer business in<br style="" class="" clear="none">> the translate methods and never deal with it outside that limited scope.<br style="" class="" clear="none">> <br style="" class="" clear="none">> <br style="" class="" clear="none">> Thank you for bringing this to my attention. I do think we need a paper<br style="" class="" clear="none">> or section in the manual on how to deal with combing GC in Lisp and<br style="" class="" clear="none">> manual memory management on the C side. <br style="" class="" clear="none">> <br style="" class="" clear="none">> When my time frees up (hopefully in a month or two) I might take a stab <br style="" class="" clear="none">> at a first draft.<br style="" class="" clear="none"><br style="" class="" clear="none">IMHO, it needs to come with a huge security warning, otherwise there will be<br style=""
class="" clear="none">endless use-after-free bugs when users lose the wrapper object after storing<br style="" class="" clear="none">the pointer somewhere.<div style="" class="" id="yqtfd93504"><br style="" class="" clear="none"><br style="" class="" clear="none">__Martin<br style="" class="" clear="none"><br style="" class="" clear="none">_______________________________________________<br style="" class="" clear="none">Cffi-devel mailing list<br style="" class="" clear="none"><a style="" class="" shape="rect" ymailto="mailto:Cffi-devel@common-lisp.net" href="mailto:Cffi-devel@common-lisp.net">Cffi-devel@common-lisp.net</a><br style="" class="" clear="none"><a style="" class="" shape="rect" href="http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel" target="_blank">http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel</a><br style="" class="" clear="none"></div><br style="" class=""><br style="" class=""></div> </blockquote> </div> </div>
</div> <div style="" class="yahoo_quoted"> </div></div></body></html>