The XS part of AlarmZone::new(SV* security_credentials, int zone) increases the reference count of security_credentials with SvREFCNT_inc() or similar, retains a pointer to the SV*, and decreases the reference count in AlarmZone->DESTROY(). This is the cleanest idea I can think of (reference counting stays in the Perl area of the program, C++ part continues unaware of Perl's existence.). I am stuck working out the details though, in particular where to store the security_credentials pointer (so it can be used later in DESTROY()).
You have to add magic to the inner blessed SV with sv_magicext, here is an example.
/* declare as 5 member, not normal 8 to save image space*/ const static struct { int (*svt_get)(SV* sv, MAGIC* mg); int (*svt_set)(SV* sv, MAGIC* mg); U32 (*svt_len)(SV* sv, MAGIC* mg); int (*svt_clear)(SV* sv, MAGIC* mg); int (*svt_free)(SV* sv, MAGIC* mg); } my_vtbl = { NULL, NULL, NULL, NULL, NULL }; /* puts newsv, refcnt++ed (caller doesn't have to do it), in sv as hid +den magic SV */ STATIC void setMgSV(pTHX_ SV * sv, SV * newsv) { MAGIC * mg; if(SvRMAGICAL(sv)) { /* implies SvTYPE >= SVt_PVMG */ mg = mg_findext(sv, PERL_MAGIC_ext, &my_vtbl); if(mg) { SV * oldsv; SvREFCNT_inc_simple_void_NN(newsv); oldsv = mg->mg_obj; mg->mg_obj = newsv; SvREFCNT_dec(oldsv); } else { goto addmg; } } else { addmg: sv_magicext(sv,newsv,PERL_MAGIC_ext,&my_vtbl,NULL,0); } } # xsub to attach a hidden SV in RV inside to the target SV of RV outsi +de # both params must be references # void SetMagicSV(outside, inside) void SetMagicSV(...) PREINIT: SV * outside; SV * inside; CODE: if(items != 2) croak_xs_usage(cv, "outside, inside"); inside = POPs; outside = POPs; PUTBACK; if(SvROK(outside) && SvROK(inside)) { outside = SvRV(outside); inside = SvRV(inside); } else{ croak_xs_usage(cv, "outside, inside"); } setMgSV(aTHX_ outside, inside); return;
The other ideas is to have the inner SV be an AV or HV instead of an SVIV as is typically for XSPP wrapped C++ objs.

Another idea is link the 2 perl objs that mirror C++ objs in pure perl in a public API pure perl obj that has refs to the 2 XS objs to make sure 1 of the XS objs doesn't go out of scope. I've seen in Perl's TAP/Test modules AUTOLOAD accessors that forward the outer obj's meth call to the inner obj's method from an AUTOLOAD-ed outer obj's method that doesn't exist in code, since you can't @ISA the inner obj class into the outer obj since the 2 objs are differently layed out internally (the outer is an hash, the inner a SVIV C++ obj). Someone else can describe more about that idea.

In reply to Re: Using multiple XS objects together by bulk88
in thread Using multiple XS objects together by sunshine4

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.