[Openmcl-devel] How to access Cocoa objects returned via indirect references (i.e. NSError)

Phil pbpublist at comcast.net
Wed Jul 26 02:41:25 PDT 2006


On Jul 25, 2006, at 2:14 AM, Gary Byers wrote:

>
> (ccl::with-autorelease-pool
>   ;; First, we (stack) allocate a pointer to a pointer to an
>   ;;  NSObject (something of foreign type :id).
>   (rlet ((error-ptr :id))
>     ;; At this point, the value of ERRROR-PTR is well-defined (it's
>     ;; an address on a stack, and if we run this in the same thread
>     ;; and in the same execution context, it'll always be pointing
>     ;; to the same address.)
>     ;; Since we haven't done anything to initialize the word of
>     ;; memory that ERROR-PTR points to, the result of doing
>     ;; (pref error-ptr :id) at this point is a little less  
> deterministic;
>     ;; the value is "whatever (garbage) was in that stack location".
>     ;; We don't care; all that we've done is to reserve a word of
>     ;; memory.  We'll pass the address of that word to an ObjC method,
>     ;; and it may store the address of some (constant or other)  
> NSError
>     ;; object there.
>     ;;
>     ;; On second thought, let's be defensive and store a NULL pointer
>     ;; there.
>     ;;
>     (setf (pref error-ptr :id) (%null-ptr))
>
>
>     (let* ((the-url-object
>               (send (@class "NSURL")
>                     :url-with-string #@"http:/blah"))
> 	   (the-xml-document
> 	       (make-objc-instance 'ns:ns-xml-document
> 		  :with-contents-of-url the-url-object
>                   :options (logior #$NSXMLNodePreserveWhitespace
>                                 #$NSXMLNodePreserveCDATA)
> 		  :error error-ptr)))
>     ;; Were we able to create an NSXMLDocument ?
>     (if (%null-ptr-p the-xml-document)
>       (error "Error creating NSXMLDocument, NSError = ~s"
>              (pref error-ptr :id))
>       the-xml-document))))
>
> The above is untested code; I'm working on something else at the  
> moment
> and can't easily test it (the only machine that I have free is running
> 10.3.9); I think that that's the general idea, however, and barring
> typos or other brain damage it should work fine.
>
> It's hard to know just by looking at a C/ObjC function prototype
> whether something passed by reference is an input, output, or both.
> In the case of this message, the parameter associated with the
> :error component of the messagee is something returned by the message,
> so the caller needs to reserve space into which the callee can store
> that extra return value.
>
> Actually, I was able to get that to work (so just beware of the fact
> that the code above neglects to use ccl:: prefixes where it  
> should ...),
> and running it produced:

Thanks very much, that filled in the blanks for me.

Phil



More information about the Openmcl-devel mailing list