I am trying to make Perl bindings for two C++ objects. In C++, their usage is:

SecurityCredentials *credentials = new SecurityCredentials(service_url +, some_secret_key); credentials->IsValid(); credentials->ChangeKey(new_secret_key); // Does occur during a normal +program as these change frequently. AlarmZone zone(credentials, 0); zone->IsArmed(); zone->SetArmed(true); AlarmZone another_zone(credentials, 1); another_zone->IsArmed(); another_zone->SetArmed(true);
As you can see, a SecurityCredentials object exists in isolation. An AlarmZone object however requires a pointer to a SecurityCredentials throughout its lifetime. Following perlxs (using ExtUtils::MakeMaker, h2xs type tools), I got the SecurityCredentials object working fine; the XS file looks like:
SecurityCredentials* SecurityCredentials::new(const char* service_url, const char* service_ +credentials) CODE: RETVAL = new SecurityCredentials(service_url, service_credentials); OUTPUT: RETVAL void SecurityCredentials::DESTROY() bool SecurityCredentials::IsValid() ...

Now comes the time to implement AlarmZone. I am hoping the usage looks like:

my $security_credentials = SecurityCredentials->new(service_url, some_ +secret_key); my $zone = AlarmZone->new($security_credentials, 1); $zone->IsArmed(); # Some time later

The reference counting of $security_credentials is my problem: For the C++ AlarmZone to work, its pointer to the C++ SecurityCredentials must be valid. However since things are running from Perl land, $security_credentials may go out of scope and be destroyed, prematurely cleaning up the underlying C++ object required by AlarmZones.

For example, in the following example AlarmZone should still work, even though $security_credentials goes out of scope:

sub SillySub { my $security_credentials = SecurityCredentials->new(service_url, som +e_secret_key); return AlarmZone->new($security_credentials, 1); }

My ideas to fix this so far have been:

Any suggestions?

Thanks! :)


In reply to 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.