Nick Ing-Simmons writes:
 > >1. Why is the return value (the pointer) of a function being reused?
 > 
 > I suspect the problem is that openlog() is not taking a copy
 > of the (mortal) string argument passed nor incrementing its REFCNT.
 > This is just a stack slot so unless perl knows what is going on it will
 > re-use the SV - typicaly doing so at the end of the "statement".

Right. I am afraid that most C-library functions work that way. The
called function can never know whether the argument is being reused or
not. Just being pessimistic and copying the arguments wouldn't lead to
very effective code. That's why usually the calling function takes
care of providing a "good" return value by allocating memory or
otherwise making sure that the pointer is not going to be reused. In
the case of an XSUB, the REFCNT could be implicitely incremented at
the beginning and decremented at the end of the XSUB. Of course this
doesn't help in the special case of openlog(). It is actually the
failure of the libc implementation or the way the syslog(3) interface
is "designed". And it is probably also the reason why Perl's XSUB
mechanism doesn't bother doing it by itself and rather pushes this
task onto the poor XSUB developer.

So I better do memory management manually. I thought about declaring a
static SV*, copying the pointer and incrementing its REFCNT in
openlog() and decrementing REFCNT in closelog().

Any opinions on that?

Best regards,
     Marcus

Reply via email to