Ludwig Arndt <[EMAIL PROTECTED]> writes:
>Dear Perl Extenders,
>
>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;

NEVER DO THAT ;-)

>
>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?

You can do something to the 'self' SV so that it is no longer 
associated with your C++ code - e.g. change pointer to NULL.
(Your C++ needs to handle a NULL this.)
Or re-bless into a class where DESTROY does nothing.


>
>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. 

You could defensively code the C++:

~Myclass()
{
  if (this) { 
   if (buf) {
     delete [] buf
     buf = 0;
   } 
  }
}

>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).
>
>
>Any Ideas where im wrong or what to do?
>
>greetings & many thanks in advance
>ludwig
>
>the files:
>
>"myclass.h"
>
>struct myclass {
>     myclass() { printf("im the myclass constructor\n");}
>     ~myclass() { printf("im the myclass destructor\n");}
>};
>
>---------------------------------------------------
>"Myclass.xs" generated by h2xs -A myclass.h
>
>#include "EXTERN.h"   /* btw. i dont(?) need the 'extern "C"'*/
>#include "perl.h"     /* statement for my compiler (g++)     */
>#include "XSUB.h"
>
>#include "ppport.h"
>
>#include <myclass.h>
>
>
>MODULE = Myclass                PACKAGE = Myclass
>
>myclass *
>myclass::new()
>
>void
>myclass::DESTROY()
>--------------------------------------------------
>"typemap"
>
>TYPEMAP
>myclass *       O_OBJECT
>
>----and this is stolen from Dean Roehrich:--------
>"perlobject.map" (relevant(?) part only)
>
>OUTPUT
>
># The Perl object is blessed into 'CLASS', which should be a
># char* having the name of the package for the blessing.
>O_OBJECT
>         sv_setref_pv( $arg, CLASS, (void*)$var );
>
>
>INPUT
>
>O_OBJECT
>         if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
>                 $var = ($type)SvIV((SV*)SvRV( $arg ));
>         else{
>                 warn( \"${Package}::$func_name() -- $var is not a 
>blessed SV reference\" );
>                 XSRETURN_UNDEF;
>         }

That warn scheme is strange.

Reply via email to