I've got two perl classes, call them A and B. A contains an array of references to B. From C (the language, not the class), I'm constructing an A, then constructing a bunch of Bs, then calling A::add( ) (written in perl, not C) to add a list of Bs. All works fine if there are fewer than 121 Bs in the list, but with more than that, something goes wrong. Depending on how I structure the code, the "something" that goes wrong can be that I get an "Attempt to free unreferenced scalar" when returning my A, or my reference to A somehow turns into &PL_sv_undef.

Some particulars:

SV *buildA(A& a) { SV *tmpSv, a_sv; I32 count; int i; dSP; ENTER; SAVETMPS; PUSHMARK; tmpSv = sv_2mortal(newSVpv("A", 0)); XPUSHs(tmpSv); PUTBACK; count = call_method("new", G_SCALAR); // A::new { // my $proto = shift; // my $class = ref($proto) || $proto; // my $this = { }; // bless($this, $class); // $this->{B} = [ ]; // return $this; // } SPAGAIN; assert(1 == count); tmpSv = POPs; a_sv = newSVsv(tmpSv); // or use NEWSV/SvSetMagicSV PUTBACK; FREETMPS; LEAVE; std::vector<SV*> b_svs; for (i = 0; i < a.b_count; ++i) { tmpSv = buildB(a.b[i]); b_svs.push_back(tmpSv); } addBs(a_sv, b_svs); return a_sv; }
buildB looks much like buildA. A::addBs looks like this:
void addBs(SV *a_sv, std::vector<SV*> &b_svs) { SV *tmpSv; I32 count; std::vector<SV*>::iterator iter; dSP; ENTER; SAVETMPS; PUSHMARK(SP); tmpSv = sv_mortalcopy(a_sv); XPUSHs(tmpSv); for (iter = b_svs.begin( ); iter != b_svs.end( ); ++iter) { XPUSHs(sv_2mortal(*iter)); } PUTBACK; count = call_method("add", G_VOID|G_DISCARD); // A::add { // my $this = shift; // push(@{$this->{B}}, @_); // } SPAGAIN; assert(0 == count); FREETMPS; LEAVE; }
If I use newSVsv in buildA/buildB, then the SV* returned by buildA( ) seems to be &PL_sv_undef. If I use NEWSV/SvSetMagicSV, then I get the "Attempt to free unreferenced scalar" in the calling XS code:
void get_a( ) PPCODE: A a = getAFromSomewhere( ); SV *a_sv = buildA(a); XPUSHs(sv_2mortal(a_sv));

In reply to Correct way to return a reference in XS/embedded perl by nikolaus

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.