On Mon, Mar 01, 2004 at 01:39:22PM +0000 Ludwig Arndt wrote:

> im new to the xsub system and playing around with a simple c++ class to
> get used to xsubs. for xs-file  and typemap see below.
> 
> struct myclass {
>      myclass() { printf("im the myclass constructor\n");}
>      ~myclass() { printf("im the myclass destructor\n");}
> };
> 
> after sending this through the xs machinery (h2xs, perl Makfile.PL etc.
> ) i get a package called Myclass (in a Module Myclass). in the following
> script i call the Myclass destructor explicitly:
> 
> use Myclass;
> 
> $x = new Myclass;
> $x->DESTROY;
> 
> and get the output
> 
> [EMAIL PROTECTED]:~/cprog/xsub/Myclass> perl  "-Iblib/lib" "-Iblib/arch" test.pl
> im the myclass constructor
> im the myclass destructor
> im the myclass destructor
> [EMAIL PROTECTED]:~/cprog/xsub/Myclass>
> 
> i conclude from this that the destructor has been called twice - once
> explicitly and once during the "global destruction" process. obviously,
> the variable $x is defined even after the (first) destructor call.
> 
> my question is: can i prevend this in the xsub?

There is nothing to prevent against. DESTROY is not special in that it
would magically destroy the object. It is just called right before the
object is destroyed by perl. This gives you an opportunity to free
memory and do some other tidying up. It was called twice for you because
you made one explicit call to it. So don't do that.

When you do this:

    $x = new Myclass;
    $x = undef;

you will notice that DESTROY is only called once, namely when $x is
assigned a new value.

> the underlying problem is that i want to wrap a c++-class that contains
> a dynamically allocated (new'ed) double array, which i have to delete[]
> in the destructor. and of course, this segfaults on the second
> destructor call, because ive deleted a pointer twice. i do not want to
> use perl AVs, because i need the class in other programs without perl.
> i cannot use swig because it didnt work (and im simply interested in
> this xs stuff).

There wont be any second call.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval

Reply via email to