sunshine4 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to make Perl bindings for two C++ objects. In C++, their usage is:
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 *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);
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! :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using multiple XS objects together
by salva (Canon) on May 09, 2013 at 08:38 UTC | |
|
Re: Using multiple XS objects together
by Anonymous Monk on May 09, 2013 at 08:29 UTC | |
by Anonymous Monk on May 09, 2013 at 08:45 UTC | |
by Anonymous Monk on May 09, 2013 at 09:06 UTC | |
|
Re: Using multiple XS objects together
by bulk88 (Priest) on May 10, 2013 at 21:38 UTC | |
by Anonymous Monk on May 10, 2013 at 22:58 UTC |