On 8/12/07, Zbigniew <[EMAIL PROTECTED]> wrote:
> Hi, just want to make sure this code is legal.  I have a C function
> which allocates a string and returns it via one of its arguments,
> expecting the caller to free it.  The following code works as
> expected, but I want to make sure it is valid.
>
> (let-location ((out c-string*))
>   (let ((rv (my-func "Lambda.OrG" #$out "Nameprep" 0)))
>     (let ((out out))           ; free out once
>       (if (eqv? rv 0)
>           (print out)
>           (error "profile error" rv)))))
>
> The issue is that ##sys#peek-and-free-c-string will be called every
> time "out" is referenced.    To guarantee "out" is freed exactly once,
> I immediately rebind out to itself after the call.  Is this a valid
> recipe?
>

I tihnk it is, look at the expansion:

csc x.scm -debug 2
[canonicalized]
(##core#callunit "library")
(##core#callunit "eval")
(##core#callunit "extras")
(##core#undefined)
(##core#undefined)
(let ()
  (let ((g2 (##core#inline_allocate ("C_a_i_bytevector" 3) '1)))
    (let ()
      (let ((rv3 (my-func
                   '"Lambda.OrG"
                   (##sys#make-locative g2 '0 '#f 'location)
                   '"Nameprep"
                   '0)))
        (let ((out4 (##sys#peek-and-free-c-string
                      (##core#inline_loc_ref (c-string*) g2)
                      '0)))
          (if (eqv? rv3 '0) (print out4) (error '"profile error" rv3)))))))
((##sys#implicit-exit-handler))
(##core#undefined)


cheers,
felix


_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to