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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.