1. The session struct contains a pointer to a string. This string is passed from the Perl code. I am assuming that perl will free string so I do not specifically free the memory associated with the string.
This is right, basically, but it's still risky business. As you have it, the string your C struct points to is the PV part1 of a scalar variable, which might have gone out of scope (and thus the PV being freed for reuse, too) at the time some of your other routines is referencing it. For example, consider the following case:
sub get_connection { # ... my $sess_id = "Session ID Goes Here"; return MySession::new($sess_id); } # $sess_id out of scope now my $x = get_connection(); my ($p, $q, $str) = MySession::get($x); # internally references PV p +art of $sess_id
This might not lead to problems right away, because Perl's memory allocation often doesn't reuse the memory immediately, but there's no guarantee whatsoever that the respective memory location will continue to hold the same string content after the associated SV has gone out of scope.
To not run into potential problems, you'd have to make sure one of the following
___
1 to verify, dump $sess_id on the Perl side using Devel::Peek, and print the address of str on the C side (printf("addr of str: %p\n", str);). You'll get something like
SV = PV(0x750b78) at 0x777ce8 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x7723c0 "Session ID Goes Here"\0 CUR = 20 LEN = 24 addr of str: 0x7723c0
Note the same address 0x7723c0.
In reply to Re: More Questions about Extending Perl
by Anonyrnous Monk
in thread More Questions about Extending Perl
by unlinker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |